Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 1st, 2008, 05:35 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default preceding-sibling::comment()

Hi, I am trying to get the value of a preceding comment. In the below example, If my context node is "foo", how do I get the comment immediately before the "foo" element.

Code:
<root>

    <data>Some Data</data>

    <foo>Some Foo</foo>
</root>
The below xslt returns all the preceding comments. I just want "This is the comment I want" .
Code:
<xsl:for-each select="foo">
 <xsl:value-of select="preceding-sibling::comment()"/>
</xsl:for-each/>
Thanks for the help.

Bones


 
Old October 1st, 2008, 05:49 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

preceding-sibling::comment()[1]

will return the most recent preceding sibling comment

preceding-sibling::node()[1][self::comment()]

will return the immediately preceding node if and only if it is a comment

preceding-sibling::node()[not(self::text()[.=''])][1][self::comment()]

will return the immediately preceding node, ignoring whitespace-only text nodes, if and only if it is a comment

Take your pick.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old October 1st, 2008, 06:33 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi Michael,

Thank you for the help. Using
Code:
preceding-sibling::node()[1][self::comment()]
Returns nothing. I am probably missing something.

Here is my actual input:

Code:
<xsl:template match="ACM:PARA">
    <fo:inline font-family="{$default-font}" font-size="{$default-font-size}">
        <xsl:apply-templates/>
    </fo:inline>
</xsl:template>
Here is my xsl:
Code:
<xsl:for-each select="//xsl:template[@match]">
            <template type="match" base="{subsequence(reverse(tokenize(document-uri(/), '/')), 1, 1)}">
                <xsl:copy-of select="@*"/>
                <xsl:attribute name="line"><xsl:value-of select="saxon:line-number()"/></xsl:attribute>
                    <xsl:attribute name="comments"><xsl:value-of select="preceding-sibling::node()[1][self::comment()]"/></xsl:attribute>
                <xsl:value-of select="@match"/>
            </template>
</xsl:for-each>
Some background - I am writing a stylesheet to describe stylesheets - Something similar to java Docs.

Thanks for the help.

 
Old October 1st, 2008, 06:46 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

There is a text node with white space between the comment and the xsl:template element. Therefore preceding-sibling::node()[1][self:comment()] is empty. I think you want preceding-sibling::comment()[1] instead.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 1st, 2008, 07:00 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks Martin,

preceding-sibling::comment()[1] selects the last preceding comment() even if its not the immediately preceding node.

Any other thoughts?

 
Old October 1st, 2008, 07:08 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It is not clear which comment nodes exactly you are looking for.
As I said, in your sample input the comment is not the immediately preceding node as there is a text node with white space between the comment and the xsl:template element.
Nevertheless I thought you want to select that comment.
Perhaps
preceding-sibling::node()[not(self::text()[normalize-space(.)=''])][1][self::comment()]
is more what you want.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 1st, 2008, 07:13 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks Martin,

Code:
preceding-sibling::node()[not(self::text()[normalize-space(.)=''])][1][self::comment()]
Works :)

What is unclear to me is the mention of whitespace. Where is the whitespace?

 
Old October 1st, 2008, 07:19 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you have
Code:
<foo/>
then the foo element node is preceded and succeeded directly by a comment node.
However with
Code:
<foo/>
there is a text node with white space between each comment node and the element node.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old October 1st, 2008, 07:27 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thank you for the clarification.

Given the following example:
Code:
<foo/>
I assumed the line break was not considered whitespace.

Thanks again,

Bones


 
Old October 1st, 2008, 08:03 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Nope, the line breaks are exactly what IS considered whitespace (along with spaces and tabs).

/- Sam Judson : Wrox Technical Editor -/




Similar Threads
Thread Thread Starter Forum Replies Last Post
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
Preceding-sibling question. lafilip XSLT 7 March 5th, 2007 12:35 PM
preceding-sibling jonesyp XSLT 2 November 22nd, 2005 12:29 PM
preceding-sibling after sort rushman XSLT 3 January 18th, 2005 11:08 AM
sorting and preceding-sibling iamhigh XSLT 5 August 25th, 2003 12:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.