HELP - how to preserve XML structure with XSLT?

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
 
B

Bjoern Hoehrmann

* n.phelge wrote in comp.text.xml:
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>

If you do not want to have your formatting changed, then why do you
instruct the writer to re-indent your content?
 
N

n.phelge

If you do not want to have your formatting changed, then why do you
instruct the writer to re-indent your content?

Sorry - that was a result of my editor pretty-print. The resulting XML
from the transform is a single line, 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 and Indentation of 0, the resulting
XML is the following:


<Head xmlns="urn:TheTest">
<Body Val1="10" Val2="11">
</Body>
</Head>


This does not validate against the schema. So, I'm looking for either
an XSLT that will allow setting the namespace and will still preserving
the XML structure of the original text, such that:


<Body Val1="10" Val2="11" />


Isn't converted to:


<Body Val1="10" Val2="11"></Body>


Or an alternative to using XmlTextWriter with Formatting.Indented and
Indentation of 0 to try to preserve the line structure of the original
XML.

Thanks in advance
 
J

Joseph Kesselman

n.phelge said:
the XML structure of the original text, such that:
<Body Val1="10" Val2="11" />
Isn't converted to:
<Body Val1="10" Val2="11"></Body>

XML considers those *COMPLETELY* equivalent. You shouldn't have to care
about which one is generated.
 
N

n.phelge

Joseph said:
XML considers those *COMPLETELY* equivalent. You shouldn't have to care
about which one is generated.

I agree completely, but when there is a linefeed between the opening
and closing element, it will not validate against the schema.

I found the solution to my problem: Instead of using
Formatting.Indented with an XmlTextWriter, I changed to specify
XmlSpace.Preserve when creating the XPathDocument:

XPathDocument xpathdocument = new XPathDocument(xmlReader,
XmlSpace.Preserve );

This preserves the formatting as I wanted.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top