English 中文(简体)
XSLT & Xpath syntax > how to refer to an element in an outer scope
原标题:

I have the following working 100% correctly. However to satisfy my curiosity... is there a way to achieve the same without declaring the currentID variable? Is there some way to reference it from within the Xpath "test" condition?

The xpath query in the condition must refer to 2 @id attributes to see if they match.

  • the current @id
  • each ancestor @id

Here s the code:

<xsl:variable name="currentID" select="@id" />
<xsl:attribute name="class">
<xsl:if test="count($currentPage/ancestor::node [@id = $currentID])&gt;0">descendant-selected </xsl:if>
</xsl:attribute>
最佳回答

Since you select the $currentID from the context node:

<xsl:variable name="currentID" select="@id" />

you can use the current() function, which always refers to the XSLT context node:

<xsl:attribute name="class">
  <xsl:if test="count($currentPage/ancestor::node[@id = current()/@id]) &gt; 0]">
    <xsl:text>descendant-selected </xsl:text>
  </xsl:if>
</xsl:attribute>

This way you don t need a variable.

A few other notes:

  • I recommend using <xsl:text> like shown above. This gives you more freedom to format your code and avoid overly long lines.
  • You don t need to do a count() > 0, simply selecting the nodes is sufficient. If none exist, the empty node-set is returned. It always evaluates to false, while non-empty node-sets always evaluate to true.

If you refer to nodes by @id regularly in your XSL stylesheet, an <xsl:key> would become beneficial:

<xsl:key name="kNodeById" match="node" use="@id" />

<!-- ... -->

<xsl:attribute name="class">
  <xsl:if test="key( kNodeById , @id)">
    <xsl:text>descendant-selected </xsl:text>
  </xsl:if>
</xsl:attribute>

The above does not need current() since outside of an XPath predicate, the context is unchanged. Also, I don t count() the nodes, since this is redundant (as explained).

问题回答

Use current() to refer to the current node processed by the template:

<xsl:if test="count($currentPage/ancestor::node [@id = current()/@id])&gt;0">

Tim got me thinking.... I think I was over complicating things, and I tried the following which works.

<xsl:if test="@id = $currentPage/ancestor::node/@id">descendant-selected </xsl:if>

XSLT seems happy comparing an attribute with a selection of attributes, and evaluating true if any of them match? if anyone has a better explanation of why this works or something better (more succinct) then put it down.

As it has already become clear, referring to the "outer scope" was not an issue, since you could do a direct comparison using the "=" operator. However, there are some cases where you do need current() and more besides where even current() doesn t cut it (because you need to "join" between more than just two contexts). In those cases, XPath 2.0 s "for" expressions are indispensable.

You could simply do:

<xsl:if test="count($currentPage[ancestor::node/@id = @id])&gt;0">descendant-selected </xsl:if>




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签