Victor Mukherjee
Victor Mukherjee

Reputation: 11035

xpath for selecting elements having a child value specified in a list

Consider the following case:

<xsl:variable name="list">
   <row>
     <webid>2</webid>
   </row>
   <row>
     <webid>3</webid>
   </row>
   <row>
     <webid>4</webid>
   </row>
</xsl:variable>

<!--pseudo code, not sure if it will work in real time-->
<xsl:variable name="addr" select="addresses/item[not(id=$list/webid)]"/>

I want to filter those addresses items which have an id not in the $list webid collection. Will the expression I posted do? Please advice.

Upvotes: 0

Views: 217

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122374

In XSLT 1.0 a variable declaration like that is a result tree fragment, not a node set, and the only thing you can do with it is copy-of or value-of - you can't navigate into it with XPath expressions. You need to either

  1. Use an extension function such as exslt:node-set to turn the RTF into a real node set or
  2. Use a trick with document('') to access the style sheet itself as an XML document

Option 2 only works in cases like your example, where the variable value is static XML. If you have a variable whose value is built using xsl: commands or attribute value templates, e.g.

<xsl:variable name="allNames">
  <xsl:for-each select="name">
    <person name="{.}" />
  </xsl:for-each>
</xsl:variable>

then you'd have to use a node-set function. The document('') approach would give you the actual xsl:for-each element and a literal value of {.} for the name attribute, rather than the result of evaluating it.

Option 1

Add xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exslt" to your <xsl:stylesheet> tag, then use

<xsl:variable name="addr" select="addresses/item[
     not(id=exslt:node-set($list)/row/webid)]"/>

Option 2

<xsl:variable name="addr" select="addresses/item[
     not(id=document('')//xsl:variable[name='list']/row/webid)]"/>

Upvotes: 2

dperez
dperez

Reputation: 101

I put not(addresses/item[id=$list/webid])

Upvotes: 0

Related Questions