XSLT: help with attributes?

Discussion in 'XML' started by Mark Tranchant, Apr 22, 2004.

  1. I'm struggling to find a way to achieve the following transformation:

    <x a="1" b="2" c="3" ... />

    into

    <y b="2" c="3" ... > <z a="1" /> </y>

    In other words, I want to pull out a specific attribute into one result
    element (easy) and copy the remaining arbitrary list of elements into
    another (help needed!).

    Any experts care to point me into the right direction?

    --
    Mark.
    http://tranchant.plus.com/
     
    Mark Tranchant, Apr 22, 2004
    #1
    1. Advertising

  2. Mark Tranchant wrote:

    > I'm struggling to find a way to achieve the following transformation:
    >
    > <x a="1" b="2" c="3" ... />
    >
    > into
    >
    > <y b="2" c="3" ... > <z a="1" /> </y>
    >
    > In other words, I want to pull out a specific attribute into one result
    > element (easy) and copy the remaining arbitrary list of elements into
    > another (help needed!).
    >
    > Any experts care to point me into the right direction?



    You usually start with the identity transformation which copies every
    node and recursively processes the child nodes, then you add templates
    matching those nodes you want to treat differently, in that case your
    <x> element.

    For instance with the example XML being

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <god>Kibo</god>
    <x a="1" b="2" c="3" />
    <devil>Xibo</devil>
    </root>

    and the XSLT stylesheet being

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" encoding="UTF-8" />

    <xsl:template match="@* | node()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
    </xsl:template>

    <xsl:template match="x">
    <y>
    <xsl:apply-templates select="@*[local-name() != 'a']" />
    <z a="{@a}" />
    </y>
    </xsl:template>

    </xsl:stylesheet>

    the result document is

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <god>Kibo</god>
    <y b="2" c="3"><z a="1"/></y>
    <devil>Xibo</devil>
    </root>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 22, 2004
    #2
    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. Max
    Replies:
    1
    Views:
    484
    Joe Kesselman
    Sep 22, 2006
  2. P4trykx
    Replies:
    2
    Views:
    1,827
    bruce barker
    Jan 31, 2007
  3. james_027

    class attributes & data attributes

    james_027, Jun 20, 2007, in forum: Python
    Replies:
    2
    Views:
    377
    Bruno Desthuilliers
    Jun 20, 2007
  4. Kyle Schmitt
    Replies:
    3
    Views:
    194
    Kyle Schmitt
    Jul 24, 2007
  5. Jayden
    Replies:
    16
    Views:
    529
    Steven D'Aprano
    Sep 29, 2012
Loading...

Share This Page