xsl - extracting corresponding pairs from different roots

R

Rob Smegma

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
 
M

Martin Honnen

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top