Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSL Transform

Reply
Thread Tools

XSL Transform

 
 
Ravi
Guest
Posts: n/a
 
      11-03-2003
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.

 
Reply With Quote
 
 
 
 
Dino Morelli
Guest
Posts: n/a
 
      11-03-2003
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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput 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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput method="text" />

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

</xsl:stylesheet>


--
Dino Morelli http://www.velocityreviews.com/forums/(E-Mail Removed) .~.
http://www.debian.org Debian GNU/Linux /V\
/( )\
^^-^^
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      11-03-2003

"Ravi" <(E-Mail Removed)> wrote in message
news:bo6btd$74d$(E-Mail Removed)...
> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
>

<xslutput 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




 
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
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
Is it possible to do a xsl transform on a string? Showjumper ASP .Net 1 07-03-2003 08:54 PM
Re: How do i transform an xml with 2 xsl files? Cowboy \(Gregory A. Beamer\) ASP .Net 1 07-03-2003 02:35 PM
What parses the xml/xsl transform in asp.net? System.xml NOT msxml? KathyB ASP .Net 0 06-25-2003 05:03 PM



Advertisments