Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Help with XSL

Reply
Thread Tools

Help with XSL

 
 
Kasp
Guest
Posts: n/a
 
      10-09-2003
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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput 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>


--
"Accept that some days you are the pigeon and some days the statue."
"A pat on the back is only a few inches from a kick in the butt." - Dilbert.



 
Reply With Quote
 
 
 
 
Martin Boehm
Guest
Posts: n/a
 
      10-09-2003
"Kasp" <(E-Mail Removed)> wrote in message
news:bm3onc$3hb$(E-Mail Removed)

> <xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">


The XSL declaration with the namespace, this is clear, I quess.

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



 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      10-09-2003
Excellent explanation!

Only two minor things:

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


Not really. "<" and "&" must be escaped, not ">".
.. . . . . . . . . . . . .

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

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


 
Reply With Quote
 
Martin Boehm
Guest
Posts: n/a
 
      10-09-2003
"Dimitre Novatchev" <(E-Mail Removed)> wrote in message
news:bm4cra$i1nm8$(E-Mail Removed)-berlin.de

> Excellent explanation!


Thanks! )

>>> <xsl:if test="count(key('pageKey', @PageKey)) > 1">

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

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

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

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


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


 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      10-09-2003

"Martin Boehm" <(E-Mail Removed)> wrote in message
news:3f85bf37$0$6599$(E-Mail Removed)-online.net...
> "Dimitre Novatchev" <(E-Mail Removed)> wrote in message
> news:bm4cra$i1nm8$(E-Mail Removed)-berlin.de
>
> > Excellent explanation!

>
> Thanks! )
>
> >>> <xsl:if test="count(key('pageKey', @PageKey)) > 1">
> >> -----------------------------------------------^
> >> This should be escaped with &gt;

> >
> > 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.
>
> >>> <xsl:apply-templates select="HierarchyMember"/>
> >>
> >> 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.

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




 
Reply With Quote
 
Kasp
Guest
Posts: n/a
 
      10-10-2003
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.
--


 
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
Help with XSL/XSL:FO for producing revision indicator bar in document. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= XML 6 05-03-2006 02:46 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
tomcal/xsl - problems with xsl:include Klaus Friese Java 0 11-22-2004 10:17 AM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 1 09-13-2004 02:28 PM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 0 09-08-2004 02:11 PM



Advertisments