• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSLT example

 
Rajan Thiyagarajan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML Source
==========
<!DOCTYPE source [
<!ELEMENT xslTutorial (doc,note*)>
<!ELEMENT doc (#PCDATA|ref)*>
<!ELEMENT ref EMPTY>
<!ATTLIST ref id IDREF #REQUIRED>
<!ELEMENT note (#PCDATA)>
<!ATTLIST note id ID #REQUIRED>
]>
<source>
<doc> This text
<ref id="n3"/> demonstrates
<ref id="n1"/> a possible usage of id function
<ref id="n2"/>.
</doc>
<note id="n1">Note n1</note>
<note id="n2">Note n2</note>
<note id="n3">Note n3</note>
</source>

XSLT stylesheet
===============
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl utput method="html"/>
<xsl:template match="/">
<xsl:apply-templates select="//doc"/>
<HR/>
<xsl:for-each select="//ref">
<xsl:apply-templates select="id(@id)">
<xsl:with-param name="nmbr">
<xsl:value-of select="position()"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="ref">
<SUP>
<xsl:value-of select="count(//doc/*) - count(following::ref)"/>
</SUP>
</xsl:template>
<xsl:template match="note">
<xsl aram name="nmbr">1</xsl aram>
<DIV>
<xsl:number value="$nmbr" format="1. "/>
<xsl:value-of select="."/>
</DIV>
</xsl:template>

</xsl:stylesheet>

If we apply the above XSLT on the XML, we will get the following output(HTML).
This text 1 demonstrates 2 a possible usage of id function 3.
--------------------------------------------------------------------------------
1. Note n3
2. Note n1
3. Note n2
Can somebody explain the flow, how the template matching taking place. This example which is given in zvon really confused me. Thanks. Rajan
 
Micheal Jacob
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajan,
For me, the XMLSPY says the xml itself is not correct. The error is "the source element is not defined in DTD".
More than that, i commented the DTD & tried to run, it is not running.i am not getting the output.

Micheal
 
Rajan Thiyagarajan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
Thanks !

Even me too got the same error when I ran with XML Spy. But I could run it successfully using the notepad editor and trying to open the XML using IE. I donot know how the flow happens. That is what I want to know. Somebody please help.
 
Micheal Jacob
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajan,
This is the flow,according to me.
1) Goes to template match"/" and calls <xsl:apply-templates select="//doc"/>
2) so it finds "This is " which is text() & prints
3) it finds <ref> tag and it has seperate template also ..so it process that.
(in just specifies 3-2,3-1,3-0) //count(ref*) that stuff
so it prints " this is " <sup>1</sup> to demo....<sup>2</sup>....
Then //ref in for-each.
if finds id(n3) As ref id is defined as IDREF, id function wont match that..so it matches note tag
with attribute id="n3" & process the corresponding template where it just prints the value that comes from param.
so it does for three times...where it prints
note 3
note 1
note 2
Micheal
 
Rajan Thiyagarajan
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Michael. That was a nice way of explaining XSLT.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic