Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xsl - extracting corresponding pairs from different roots

Reply
Thread Tools

xsl - extracting corresponding pairs from different roots

 
 
Rob Smegma
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-04-2005


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 xslutput method text.
Here is an example stylesheet

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

<xslutput 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/
 
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
Implementing a pricing function (Extracting Square Roots) Falk Köppe Java 6 12-16-2009 02:44 PM
XSLT - Extracting name-value pairs Ebenezer XML 11 11-10-2008 09:07 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Extracting Numerica Data Pairs from Text Box Michael Hill Javascript 5 02-15-2005 06:45 AM
Re: Application Roots for assemblies Kevin Spencer ASP .Net 5 07-04-2003 11:21 AM



Advertisments