I am using XLST 1.0 and want to Transform XML to add 'nil' attributes to empty elements. I am finding that the namespace is being added to each matching element e.g my output looks like:
<age xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
I know this is valid but I'd rather it was added to my top-node. I came across this answer: How can I add namespaces to the root element of my XML using XSLT?
However I have multiple possible root nodes, so I thought I could do something like this:
<xsl:template match="animals | people | things">
<xsl:element name="{name()}">
<xsl:attribute name="xmlns:xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
However I get an error from Visual Studio "prexix xmlns not defined" and I'm not sure what to do about this.
Here is my total XLST file (it won't paste into SO for some reason) which tries to do a few things:
- Transform different types of animal into a single type
- Add the namespace to the root node
- Add
xsi:nil = true
to empty elements (note they must have no children not just no text, or my top-level node gets transformed)