Simple xslt question

Discussion in 'XML' started by jon.harding@gmx.co.uk, Mar 6, 2012.

  1. Guest

    Hi, I have XML that looks like this

    <xml version="1.0" encoding="UTF-8">
    <Foo xmlns:xsi:=htttp;//www.w3.org/2001/etc>
    <Baa1>
    <Baa11>baa</Baa11>
    <Baa12>baa</Baa12>
    </Baa1>
    <Baa3>
    <Baa38>baa</Baa38>
    </Baa3>
    <Baa4>baa</Baa4>
    </Foo>

    and what I want to turn it in to with xslt is

    <Foo>
    <Baa1>
    <Baa11>baa</Baa11>
    <Baa12>baa</Baa12>
    </Baa1>
    <Baa3>
    <Baa38>baa</Baa38>
    </Baa3>
    <Baa4>baa</Baa4>
    </Foo>

    with generic xslt. The XML will have a different structure, the root
    nodes won't always be Foo and the children won't always look like Baa,
    they will always be different. My requirement is to strip out the
    namespaces and <?xml etc> in a generic sense.

    Is it possible to write generic xslt to strip out all namespaces (ALL
    OF THEM, and some elements in the XML tree may have other namespaces
    that are not w3.org) and <?xml xxxxx> leaving everything else in there

    Thank you for your help

    Jon
     
    , Mar 6, 2012
    #1
    1. Advertising

  2. Mayeul Guest

    On 06/03/2012 16:21, wrote:
    > with generic xslt. The XML will have a different structure, the root
    > nodes won't always be Foo and the children won't always look like Baa,
    > they will always be different. My requirement is to strip out the
    > namespaces and<?xml etc> in a generic sense.
    >
    > Is it possible to write generic xslt to strip out all namespaces (ALL
    > OF THEM, and some elements in the XML tree may have other namespaces
    > that are not w3.org)


    Sure:

    <xsl:template match="*">
    <!-- Create element with same local name and no namespace -->
    <xsl:element name="{local-name()}">
    <!-- Add the attributes and apply the same to the children -->
    <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
    <!-- Create attribute with same local name and value -->
    <xsl:attribute name="{local-name()}"><xsl:value-of
    select="."/></xsl:attribute>
    </xsl:template>


    > and<?xml xxxxx> leaving everything else in there


    Theoretically that's what

    <xsl:eek:utput method="xml" omit-xml-declaration="yes"/>

    is about.
    But XML toolchains have an habit of randomly reinserting the XML
    declaration here and there, and therefore you'll sometimes find it back
    in the produced document anyway. It all depends on your toolchain.

    --
    Mayeul
     
    Mayeul, Mar 6, 2012
    #2
    1. Advertising

  3. Possible? Yes. Wise? Almost never.

    Namespaces are part of the semantics of the document. Remove them and
    the meaning of the document changes. Consider them a shorthand
    replacement for including the URI in every instance of the name.
     
    Joe Kesselman, Mar 8, 2012
    #3
    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. Stylus Studio
    Replies:
    0
    Views:
    687
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,691
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    625
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    724
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    18
    Views:
    2,762
    Joseph Kesselman
    Oct 4, 2006
Loading...

Share This Page