HELP - how to preserve XML structure with XSLT?

Discussion in 'XML' started by n.phelge, Jan 18, 2007.

  1. n.phelge

    n.phelge Guest

    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
    n.phelge, Jan 18, 2007
    #1
    1. Advertising

  2. * 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 · mailto: · 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/
    Bjoern Hoehrmann, Jan 18, 2007
    #2
    1. Advertising

  3. n.phelge

    n.phelge Guest

    > 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
    n.phelge, Jan 18, 2007
    #3
  4. 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
    Joseph Kesselman, Jan 18, 2007
    #4
  5. n.phelge

    n.phelge Guest

    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
    n.phelge, Jan 18, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Dalessandro
    Replies:
    4
    Views:
    902
    Luke Dalessandro
    Sep 24, 2004
  2. sim.sim
    Replies:
    18
    Views:
    1,998
    Maksim Kasimov
    May 25, 2007
  3. Dan Stromberg
    Replies:
    2
    Views:
    932
    Stefan Behnel
    Sep 26, 2007
  4. Joe Fawcett
    Replies:
    0
    Views:
    359
    Joe Fawcett
    Nov 21, 2007
  5. Grzegorz Chrupala
    Replies:
    2
    Views:
    192
    Grzegorz Chrupala
    Jun 30, 2003
Loading...

Share This Page