I'm trying to do something very similar, and it's something that seems like it should be easy. The use case is returning the full outer XML of an XML element - or to clone a node. For example, if I have an XML document that looks like this:
<MyZoo>
<MyAnimals>
<Animal id="1">
<Species>Lion</Species>
<Name>Leo</Name>
<Age>12</Age>
</Animal>
<Animal id="2">
<Species>Bear</Species>
<Name>Smokey</Name>
<Age>3</Age>
</MyAnimals>
</MyZoo>
Say I want to return to a user one distinct animal. I can use the XML extension to get a single element:
But how do I get that XML returned is text? I don't want the entire document returned. I could technically almost rebuild the XML by creating a new XML document, cycling through the nodes of the old document, creating elements of the right name, etc. I'm not sure how I would get all of the attributes of a specific node, though. In the example case, I would lose the id attribute of the first node.
And in the real-life use case I'm working with, I don't know the structure of the XML underneath the element I'm trying to clone, so I wouldn't be able to map each attribute by name.
Is my only option to use XSL? I know how to do this in C#, but I'm trying not to write another extension.
We created a module to add some features we're missing, and this was one of them. So far we've been able to do it without any extension/C# code. My solution was to create an action that took the XML Elment as an input. It creates a temporary "tag" attribute, runs it through an XSL transform that looks for the tagged element and outputs a new document with just that element, loads it into a dom, then returns the output of an _Save. Works great.
Here's the XSL:
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="3.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform" xmlns:xs="https://www.w3.org/2001/XMLSchema" xmlns:fn="https://www.w3.org/2005/xpath-functions"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:apply-templates select="//*[@" + TaggerAttributeName + "]" /> </xsl:template> <xsl:template match="*" > <xsl:copy-of select="." />
</xsl:template></xsl:stylesheet>