473,395 Members | 2,796 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,395 software developers and data experts.

XSL Transform

I am new to XSL. I have a XML document something like

<abc>
xyz
<def> 1 </def>
<def> 2 </def>
<def> 3 </def>
<def> 4 </def>
<def> 5 </def>
</abc>

I am trying to get the value of the abc node which in this case is xyz.
However if I try to get the value using <xsl:value-of select="."/> in a
template matching abc I get

xyz 1 2 3 4 5

which is not what I want. Is there someway to select only the value of
the parent node and then display each child node's value on say a
separate line? I tried using concat and substring-before and though it
works its not a good solution as I do not know how many children abc has
in advance (besides being very laborious). Any suggestions will be
highly appreciated.

TIA.

Jul 20 '05 #1
2 2145
On Mon, 3 Nov 2003, Ravi wrote:
I am new to XSL. I have a XML document something like

<abc>
xyz
<def> 1 </def>
<def> 2 </def>
<def> 3 </def>
<def> 4 </def>
<def> 5 </def>
</abc>

I am trying to get the value of the abc node which in this case is xyz.
However if I try to get the value using <xsl:value-of select="."/> in a
template matching abc I get

xyz 1 2 3 4 5

which is not what I want. Is there someway to select only the value of
the parent node and then display each child node's value on say a
separate line? I tried using concat and substring-before and though it
works its not a good solution as I do not know how many children abc has
in advance (besides being very laborious). Any suggestions will be
highly appreciated.


To get all of the text values of all of the children of <abc> on
separate lines you can loop through all children:

<?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" />

<xsl:template match="/">
<xsl:for-each select="*">
<xsl:value-of select="." />
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
To get just the single text value of <abc> without the <def> children at
all you can use the text() function:

<?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" />

<xsl:template match="/">
<xsl:value-of select="/abc/text()" />
</xsl:template>

</xsl:stylesheet>
--
Dino Morelli di**********@snet.net .~.
http://www.debian.org Debian GNU/Linux /V\
/( )\
^^-^^
Jul 20 '05 #2

"Ravi" <rg**@cse.buffalo.edu> wrote in message
news:bo**********@prometheus.acsu.buffalo.edu...
I am new to XSL. I have a XML document something like

<abc>
xyz
<def> 1 </def>
<def> 2 </def>
<def> 3 </def>
<def> 4 </def>
<def> 5 </def>
</abc>

I am trying to get the value of the abc node which in this case is xyz.
No, the string value of an element is the concatenation of all of its
descendent text nodes:

From the XPath spec:

"The string-value of an element node is the concatenation of the
string-values of all text node descendants of the element node in document
order."

http://www.w3.org/TR/xpath#element-nodes

However if I try to get the value using <xsl:value-of select="."/> in a
template matching abc I get

xyz 1 2 3 4 5
This is the correct string value of the element (or root node) according to
the XPath spec.

which is not what I want. Is there someway to select only the value of
the parent node and then display each child node's value on say a
separate line?
By "value of the parent node" I guess you mean the value of its first text
node child?

Yes, this transformation produces the results you want:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:output method="text"/>
<xsl:strip-space elements="*" />

<xsl:template match="text()">
<xsl:value-of select="."/>
<xsl:text>&#xA;</xsl:text>
</xsl:template>
</xsl:stylesheet>

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


Jul 20 '05 #3

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

Similar topics

5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
0
by: Xiaolei Li | last post by:
first off, i'm a total newbie at this stuff so excuse any wrong usage of terminology or whatever else. i have a XSL to transform a Document such that all "text" nodes will have a "SPAN" inserted...
2
by: John Lehmann | last post by:
I have an interesting problem. I am performing an XSL transform using the System.Xml.Xsl.Transform class. I have a database that contains the XSL style sheet string. And it seems to work pretty...
9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
3
by: Jason S | last post by:
Hello Group, I am just about tearing my hair out with this one and thought someone may have some insight. I have a transform that wasn't working so I grabbed the nearest debugger (xselerator)...
4
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
1
by: Mike Hofer | last post by:
I've got two statements in my code that are both generating weird, weird, weird messages: The first one was, Dim document As System.Xml.XmlDocument Dim navigator As...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
6
by: Vijai Kalyan | last post by:
Hello, I am trying to use std::transform to take in a collection of strings, transform them and insert the result into an output container. So, I wrote something like this: std::wstring...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.