N
n.phelge
I need to perform an XSLT to set the namespace on some XML and I need
to preserve the original document line formatting to assist with error
handling (so the line number from any schema validation error matches
the original line number). The original XML looks like this:
<Head>
<Body Val1="10" Val2="11" />
</Head>
and the XSD looks like this:
<xs:schema xmlns="urn:TheTest"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:TheTest" elementFormDefault="qualified">
<xs:element name="Head">
<xs:complexType>
<xs:sequence>
<xs:element name="Body" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="Val1" type="xs:string"/>
<xs:attribute name="Val2" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The XSLT I'm applying to set the namespace to the topmost element for
validation looks like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns="urn:TheTest">
<!-- This loop will just set the namespace of the top element -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
The problem is that the resulting XML from the transform looks like
this:
<Head xmlns="urn:TheTest">
<Body Val1="10" Val2="11"></Body>
</Head>
So when I write the XML using an XmlTextWriter in .NET specifying a
Formatting of Formatting.Indented, the resulting XML is the following:
<Head xmlns="urn:TheTest">
<Body Val1="10" Val2="11">
</Body>
</Head>
This does not validate against the schema, since the Body element
shouldn't have any content. Does anyone else have a better approach to
perform an XLST that will set the namespace of the topmost element and
still preserve the formatting and line structure of the source XML? Any
help would be appreciated. I don't have the option of restructuring
the schema or forcing the customer to change their XML - this is a
legacy interface that must be maintained, and I need to add the ability
to validate the XML by assigning a namespace.
Thanks in advance
to preserve the original document line formatting to assist with error
handling (so the line number from any schema validation error matches
the original line number). The original XML looks like this:
<Head>
<Body Val1="10" Val2="11" />
</Head>
and the XSD looks like this:
<xs:schema xmlns="urn:TheTest"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:TheTest" elementFormDefault="qualified">
<xs:element name="Head">
<xs:complexType>
<xs:sequence>
<xs:element name="Body" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="Val1" type="xs:string"/>
<xs:attribute name="Val2" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The XSLT I'm applying to set the namespace to the topmost element for
validation looks like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns="urn:TheTest">
<!-- This loop will just set the namespace of the top element -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
The problem is that the resulting XML from the transform looks like
this:
<Head xmlns="urn:TheTest">
<Body Val1="10" Val2="11"></Body>
</Head>
So when I write the XML using an XmlTextWriter in .NET specifying a
Formatting of Formatting.Indented, the resulting XML is the following:
<Head xmlns="urn:TheTest">
<Body Val1="10" Val2="11">
</Body>
</Head>
This does not validate against the schema, since the Body element
shouldn't have any content. Does anyone else have a better approach to
perform an XLST that will set the namespace of the topmost element and
still preserve the formatting and line structure of the source XML? Any
help would be appreciated. I don't have the option of restructuring
the schema or forcing the customer to change their XML - this is a
legacy interface that must be maintained, and I need to add the ability
to validate the XML by assigning a namespace.
Thanks in advance