Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSLT Question

 
Kris Mann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
This is my maiden post ...
XSL is as follows:
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<xsl:apply-templates select="id('intro body end')"/>
<P>
<xsl:value-of select="id('intro body end')"/>
</P>
<P>
<xsl:value-of select="id('body end intro')"/>
</P>
<P>
<xsl:value-of select="id('in bod end')"/>
</P>
</xsl:template>
<xsl:template match="*">
<P style="color:red">
<xsl:value-of select="."/>
</P>
</xsl:template>

</xsl:stylesheet>
-----------------------XML is as below --------
<!DOCTYPE source [
<!ELEMENT chapter ANY>
<!ATTLIST chapter id ID #REQUIRED>
<!ELEMENT title ANY>
<!ATTLIST title id CDATA #REQUIRED>
<!ELEMENT text ANY>
<!ATTLIST text value ID #REQUIRED>
]>
<source>
<chapter id="intro">Introduction</chapter>
<chapter id="body">
<title id="t1">BODY</title>
<text value="text1">text text text</text>
</chapter>
<chapter id="end">THE END</chapter>
</source>
********* Output (on Saxon) is as below ******
<P style="color:red">Introduction</P>
<P style="color:red">
BODY
text text text
</P>
<P style="color:red">THE END</P>
<P>Introduction</P>
<P>Introduction</P>
<P>THE END</P>
How does <P>Introduction</P> (penultimate line in the output appear). Can somebody explain
 
Siva shanmugam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be because of the place you are using the id function.
In <xsl:apply-templates> it selects all the text value of the nodes.
In <xsl:value-of> it selects the only first node's text value which is in the resultant node set.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you were expecting the following for the penultimate line -
===========================================
BODY
text text text
INSTEAD OF .............
Introduction
============================================
The reason for this is that the function
id("body intro end") will select 3 nodes as it traverses "down the tree". This is the reason why "intro" stuff always happens to be the first node.
Please run this simple stylesheet to confirm this -
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<P>
<xsl:for-each select="id('body intro end')">
<xsl:value-of select="."/>
</xsl:for-each>
</P>
</xsl:template>
<xsl:template match="*">
<P style="color:red">
<xsl:value-of select="."/>
</P>
</xsl:template>
</xsl:stylesheet>
 
Kris Mann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give that a try, and I will get back to you
 
Kris Mann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks a bunch Jayadev,
That works for me .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic