Sawtooth67
Sawtooth67

Reputation: 83

How to match values in variable in predicate in XPATH

I have a working XSLT 2 or 3 process where I pass a single word string as a parameter into the XSL template. For this task I'm passing a surname which is used to find all <person> elements in multiple XML documents where it's child <nL> ,name last, element matches the parameter I've passed. This works but I'd like to modify my XPATH expression to be able to pass a comma separated parameter. For example, I can now pass surname="Johnson" but would like to be able to pass surname="Johnson,Johnston"

I'm using the latest Saxon processor in java. I invoke the transformation with

java net.sf.saxon.Transform -s:all-sources.xml -xsl:xsl/index-person-param.xsl surname="Johnson"

all-sources.xml is an XML formated list of other XML files.

I will only include a small section of my code unless more is needed. This is likely a simple XPATH issue that I just don't understand.

Currently before my template match I have this parameter declaration

<xsl:param name="surname" as="xs:string" required="yes"/>

then I find all the matching <person> elements with the following variable

<xsl:variable name="nameMatch" select="document(/files/file/@filename)//person[nL = $surname]"/>

This works when the $surname variable is a one-word string. How could I modify the predicate above so that $surname can contain two or more words separated by a comma?

I've tried several alterations using tokenize() and contains() but I've still not found a working solution. Thanks, Michael

Upvotes: 0

Views: 1151

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167516

If you keep <xsl:param name="surnames" as="xs:string" required="yes"/> then use tokenize to create a sequence of strings with e.g. <xsl:param name="surname-seq" as="xs:string*" select="tokenize($surnames, '\s*,\s*')"/> and then compare nL = $surname-seq.

Or change the parameter to be a sequence of strings with e.g. <xsl:param name="surname" as="xs:string*" required="yes"/>, then, on the command line you need to make sure you use e.g. ?surname="'foo','bar'". You will have to test which combinations of quotes work, the question mark ?surname indicates the value is an XPath expression, it then depends on the command shell you use what you need to make sure you pass a complete expression to Saxon.

Upvotes: 2

Related Questions