Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > HELP - how to preserve XML structure with XSLT?

Reply
Thread Tools

HELP - how to preserve XML structure with XSLT?

 
 
n.phelge
Guest
Posts: n/a
 
      01-18-2007
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"
xmlnss="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 xmlnssl="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

 
Reply With Quote
 
 
 
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      01-18-2007
* 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?
--
Björn Höhrmann · (E-Mail Removed) · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
 
Reply With Quote
 
 
 
 
n.phelge
Guest
Posts: n/a
 
      01-18-2007
> 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

 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      01-18-2007
n.phelge wrote:
> 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.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
n.phelge
Guest
Posts: n/a
 
      01-18-2007

Joseph Kesselman wrote:
> n.phelge wrote:
> > 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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Help - how to preserve XML Structure using C++ XSL Transform? Joe Fawcett C++ 0 11-21-2007 10:43 AM
Python XML API's that preserve XML comments? Dan Stromberg Python 2 09-26-2007 07:27 AM
xml.dom.minidom: how to preserve CRLF's inside CDATA? sim.sim Python 18 05-25-2007 04:03 PM
FileUtils - :preserve does not preserve mtime of directories on Windoze Grzegorz Chrupala Ruby 2 06-30-2003 07:38 AM



Advertisments