473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

xsl - extracting corresponding pairs from different roots

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

Nov 3 '05 #1
1 1224


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:output 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:output 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/
Nov 4 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: svilen | last post by:
hi. this was named but it is misleading. i want to have the order of setting items in intrinsic dicts (keyword args, or class attributes, etc). which is, a prdered dict trhat matches the...
2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
82
by: zardoz | last post by:
I've got this problem: unsigned long long lTemp; char cLargeNum="1324567890"; sscanf(clargeNum,"%llu",&lTemp); which under Win32 isn't working*. My program needs to compile under posix so...
3
by: Abhi | last post by:
Hi- I'm looking for C# code sample that will extract the list of websites from the IIS manager. anyone? Thanks!
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
2
by: =?ISO-8859-1?Q?Sch=FCle_Daniel?= | last post by:
Hello NG, given this call to roots funtion from pylab In : roots() Out: array() as far as I understand it stands for a0+a1*x+a2*x^2 in the above case it yields 2x^2+2x = 2x(1+x) and the...
15
by: mcjason | last post by:
I saw something interesting about a grid pair puzzle problem that it looks like a machine when you find each that work out the way it does and say with all the others that work out the way they...
2
by: clawsicus | last post by:
Hi list, I'm trying to use regular expressions to help me quickly extract the contents of messages that my application will receive. I have worked out most of the regex but the last section of...
11
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.