xsl - extracting corresponding pairs from different roots

Discussion in 'XML' started by Rob Smegma, Nov 3, 2005.

  1. Rob Smegma

    Rob Smegma Guest

    Greetings,

    I have an XML document like so:

    <root>
    <head>
    <meta name=1/>
    <meta name=2/>
    <meta name=3/>
    </head>
    <body>
    <msg>HEY</msg>
    <msg>YOU</msg>
    <msg>THERE</msg>
    </body>
    </root>

    I would like to transform this into:

    (1,HEY), (2,YOU), (3,THERE)

    The meta elements correspond to the msg elements.

    I am an XSL newbie so I'm still trying to learn this technology. Any
    help would be greatly appreciated! Thank you.

    Robert
    Rob Smegma, Nov 3, 2005
    #1
    1. Advertising

  2. Rob Smegma wrote:


    > <root>
    > <head>
    > <meta name=1/>


    Should be
    <meta name="1" />
    then to be well-formed, attribute values need to be quoted.
    Same below for the other meta elements.

    > <meta name=2/>
    > <meta name=3/>
    > </head>
    > <body>
    > <msg>HEY</msg>
    > <msg>YOU</msg>
    > <msg>THERE</msg>
    > </body>
    > </root>
    >
    > I would like to transform this into:
    >
    > (1,HEY), (2,YOU), (3,THERE)
    >
    > The meta elements correspond to the msg elements.


    Process the meta elements and then use the position (or perhaps the name
    attribute, not clear from your description what establishes the
    relationship) to find the corresponing msg element.
    As you seem to want to have text output then use xsl:eek:utput method text.
    Here is an example stylesheet

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

    <xsl:eek:utput method="text" media-type="text/plain" encoding="UTF-8" />

    <xsl:variable name="messages" select="/root/body/msg" />

    <xsl:template match="root">
    <xsl:apply-templates select="head/meta" />
    </xsl:template>

    <xsl:template match="meta">
    <xsl:variable name="index" select="position()" />
    <xsl:text>(</xsl:text>
    <xsl:value-of select="concat(@name, ',', $messages[$index])" />
    <xsl:text>)</xsl:text>
    <xsl:if test="position() != last()">
    <xsl:text>, </xsl:text>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 4, 2005
    #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. Kevin Spencer

    Re: Application Roots for assemblies

    Kevin Spencer, Jul 3, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    421
    Ian Turner
    Jul 4, 2003
  2. Replies:
    1
    Views:
    3,585
    A. Bolmarcich
    May 27, 2005
  3. Ebenezer
    Replies:
    11
    Views:
    1,120
    Ebenezer
    Nov 10, 2008
  4. Falk Köppe
    Replies:
    6
    Views:
    941
  5. Michael Hill

    Extracting Numerica Data Pairs from Text Box

    Michael Hill, Feb 10, 2005, in forum: Javascript
    Replies:
    5
    Views:
    202
    Michael Hill
    Feb 15, 2005
Loading...

Share This Page