user1136003
user1136003

Reputation: 99

XPath 1.0 : Searching over all the elements with contains

I have an XML document with the following structure :

<books>
  <book>
    <title/>
    <author></author>
    ...
  </book>
</books>

Now I would like to find all books of an author. I use the following XPath 1.0 expression :

/books/book[contains(author,$value)]

But this will only returns me all the books where author is on first place (first author-element of a book-element).

How can I achieve it to find also the books where the author is not on first place?
I'm using XSLT 1.0 in Firefox.

Upvotes: 2

Views: 681

Answers (2)

zx485
zx485

Reputation: 29022

In XSLT-1.0 you cannot use a variable in a template matching expression.
It just doesn't work, so either hard-code the value or put it in an xsl:if or xsl:choose.

So

<xsl:template match="/books/book">
    <xsl:if test="contains(author/text(),$value)">
        CONDITION Fulfilled!
    </xsl:if>
</xsl:template>

does work, but using it as a predicate

<xsl:template match="/books/book[contains(author/text(),$value)]">
  CONDITION won't work!
</xsl:template>

would not, unless you hard-code the matching VALUE like

<xsl:template match="/books/book[contains(author/text(),'VALUE')]">
  CONDITION won't work!
</xsl:template>

Upvotes: 0

kjhughes
kjhughes

Reputation: 111541

This XPath,

/books/book[author[contains(.,$value)]]

will select all book elements with an author child element whose string value contains the substring, $value.


This XPath,

/books/book[author = $value]

will select all book elements with an author child element whose string value is $value.

Upvotes: 1

Related Questions