• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another question on XSL

 
Dhiren Joshi
Ranch Hand
Posts: 463
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
<xsl:apply-templates select="//CCC"/>
</xsl:template>

<xsl:template match="CCC">
<H3 style="color:blue">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H3>
</xsl:template>

<xsl:template match="AAA/CCC/CCC">
<H2 style="color:green">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>


<xsl:template match="CCC/CCC">
<H2 style="color:red">
<xsl:value-of select="name()"/>
<xsl:text> (id=</xsl:text>
<xsl:value-of select="@id"/>
<xsl:text> )</xsl:text>
</H2>
</xsl:template>

</xsl:stylesheet>


XML

<?xml version="1.0"?>
<test>

<AAA id='a1' pos='start'>
<BBB id='b1'/>
<BBB id='b2'/>
</AAA>
<AAA id='a2'>
<BBB id='b3'/>
<BBB id='b4'/>
<CCC id='c1'>
<CCC id='c2'/>
</CCC>
<BBB id='b5'>
<CCC id='c3'/>
</BBB>
</AAA>
</test>

a. The out will have blue color.
b. The out will have red color
c. The out will have green color
d. The out will have green and red
e. The out put will have blue and green

Please select 2 answers.

Please explain the answers.. Thanks

Thanks
Dhiren
 
Jay Blanton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you translate this?

The answer is a and b. Red and blue.

The output looks like this:

This apply-templates says find any CCC element in the document.

It first finds

It looks for a template to match CCC with id='cd1'. It finds the following:

So the first color is blue. Then it finds:

It looks for a template to match CCC with id='cd2'. It finds the following:

Both of these templates match. But according to the XSLT Reference (by Michael Kay page 315)..."If there are several matching templates left, and they all have the same import precedence and priority, the XSLT processor can either choose the one that occurs last in the stylesheet, or report an error."

So the XSLT picks the:

Because it is last in the XSLT, so the next color is red. If you switch the order of

The colors will change.

Then it finally finds:

It looks for a template to match CCC with id='cd3'. It finds the following:

So the color is blue again.

Hope this helps.

- Jay
[ June 17, 2004: Message edited by: Jay Blanton ]
 
Dhiren Joshi
Ranch Hand
Posts: 463
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jay,
Yes I translated it and it is correct it is blue and red.
I am not sure how u say both the templates are same in priority. Thats my confusion. AAA/CCC/CCC and CCC/CCC .. How r they sam e..
Thanks
Dhiren
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dhiren Joshi:
I am not sure how u say both the templates are same in priority.

The XSL specification clearly states the heuristics for determining priority between templates. Have a look at here.
 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jay and Lasse for your explanations.
that really helped me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic