Help with XSL

Kasp

Usenet Archive
Joined
Oct 3, 2003
Messages
22
Reaction score
0
Hi,
I have an XSL here...and though I understand XML, I am having a hard time understanding what this XSL does? Any help will be appreciated.
TIA

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:eek:utput indent="yes"/> <xsl:key name="pageKey" match="//HierarchyMember" use="@PageKey"/> <xsl:template match="/"> <Root> <xsl:apply-templates select="//HierarchyMember"/> </Root> </xsl:template> <xsl:template match="HierarchyMember"> <xsl:if test="count(key('pageKey', @PageKey)) > 1"> <xsl:copy> <xsl:attribute name="PageKey"><xsl:value-of-select="@PageKey"/></xsl:attribute> <xsl:attribute name="Count"><xsl:value-of select="count(key('pageKey',@PageKey))"/></xsl:attribute> </xsl:copy> </xsl:if> <xsl:apply-templates select="HierarchyMember"/> </xsl:template>

</xsl:stylesheet>
 

Martin Boehm

Usenet Archive
Joined
Sep 12, 2003
Messages
52
Reaction score
0
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

The XSL declaration with the namespace, this is clear, I quess.
<xsl:eek:utput indent="yes"/>

Advises the XSL Processor to indent output tags to make them somewhat more readable.
<xsl:key name="pageKey" match="//HierarchyMember" use="@PageKey"/>

Creates a key (think of an associative array) named pageKey from all HierarchyMember elements anywhere in the document, using their PageKey attribute as association.
<xsl:template match="/">
<Root>
<xsl:apply-templates select="//HierarchyMember"/>
</Root>
</xsl:template>

Matches the root node, puts a Root element around the output and begins processing all HierarchyMember elements in the documant one by one.

The next template is executed once per HierarchyMember element:
<xsl:template match="HierarchyMember">
<xsl:if test="count(key('pageKey', @PageKey)) > 1">
------------------------------------------------^
This should be escaped with &gt;

Tests if there is more than one HierarchyMember element with a PageKey attribute equal to the currently processed HierarchyMember. The key function returns all node of the previously created key that have the value of the current PageKey attribute associated with them.

If this is true, copy the current element into the output:
<xsl:copy>

....but add two attributes - one containing the current PageKey:
<xsl:attribute name="PageKey">
<xsl:value-of-select="@PageKey"/>
</xsl:attribute>

....and one containing the count of the HierarchyMember elements havin the same PageKey as the current one:
<xsl:attribute name="Count">
<xsl:value-of select="count(key 'pageKey',@PageKey))"/> </xsl:attribute> </xsl:copy> </xsl:if>

End if.
<xsl:apply-templates select="HierarchyMember"/>

This is senseless. The template is already repeatedly called by the one that matches the document root.
</xsl:template>

End of HierarchyMember template.
</xsl:stylesheet>

End of stylesheet.

What the styleheet does is counting how many HierarchyMember elements with duplicate PageKeys exist in the source .

I don't know if it is a bug, but I guess: The xsl:if will cause duplicate HierarchyMember elemets to appear in the output, as may as there are duplicate ones in the input. That means, if there are two HierarchyMember's having PageKey="1", the outpot will be

<HierarchyMember PageKey="1" Count="2" />
<HierarchyMember PageKey="1" Count="2" />

because there are two elements passing the "count(key('pageKey', @PageKey)) > 1" test, which are processed one by one, so the condition is true twice during the transformation process.

Martin
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
Excellent explanation!

Only two minor things:
The next template is executed once per HierarchyMember element:

------------------------------------------------^
This should be escaped with &gt;

End if.

This is senseless. The template is already repeatedly called by the one that matches the document root.

Yes this is obviously an error, but depending on the source.xml this may or may not affect the result of the transformation.

For example, if a "HierarchyMember" does not have children which are "HierarchyMember", then the above xsl:apply-templates will not be applied on any nodes.

=====
Cheers,

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

Martin Boehm

Usenet Archive
Joined
Sep 12, 2003
Messages
52
Reaction score
0
Excellent explanation!

Thanks! :eek:)
Not really. "<" and "&" must be escaped, not ">".

I could say "That's why I said `should`." now. ;-)
But actually I was not aware of that (as I always escape tag brackets), and I did not run the code.
Yes this is obviously an error, but depending on the source.xml this may or may not affect the result of the transformation.

For example, if a "HierarchyMember" does not have children which are "HierarchyMember", then the above xsl:apply-templates will not be applied on any nodes.

I am not sure if I get that. You say it will _not_ be applied if there are no matching children. That is clear. This implies it would be applied if there were, that is clear, too.

But I can't see what sense it would make (at least in this context) to recursively apply the template, as it is already applied to each and every HierarchyMember by the first (root-matching) one. Would that not produce even more duplicate output, as any child HierarchyMember is hit redundantly?

Martin
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
Martin Boehm said:
Thanks! :eek:)

I could say "That's why I said `should`." now. ;-)
But actually I was not aware of that (as I always escape tag brackets), and I did not run the code.

I am not sure if I get that. You say it will _not_ be applied if there are no matching children. That is clear. This implies it would be applied if there were, that is clear, too.

But I can't see what sense it would make (at least in this context) to recursively apply the template, as it is already applied to each and every HierarchyMember by the first (root-matching) one. Would that not produce even more duplicate output, as any child HierarchyMember is hit redundantly?

At the scope of the:
<xsl:apply-templates select="HierarchyMember"/>

the current node is an "HierarchyMember" element.

If no "HierarchyMember" element has a "HierarchyMember" child, then the "select" attribute above will not select any node and, therefore, no template will be applied.

Therefore, in this case no additional (duplicate) output will be produce.

Of course, the above xsl:apply-templates is an error -- we only talk here whether the output transformation will be affected by this error.

Unfortunately, for some source xml documents with the structure described above the output of the transformation will not be affected by the error -- thus the programmer may not notice this meaningless and erroneous xsl instruction.

=====
Cheers,

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

Kasp

Usenet Archive
Joined
Oct 3, 2003
Messages
22
Reaction score
0
Thanks Martin for your time and explanation - which was very well explained.
Thanks to Dimtre too.

I understand that the <xsl:apply-templates select="HierarchyMember"/> in <xsl:template match="HierarchyMember"> is redundant.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top