I have some XML like this:
<topics>
<topic id="50"/>
<topic id="51"/>
<topic id="52"/>
</topics>
<discussions>
<discussion type="foo">talked about T50 as Discussion 1000</discussion>
<discussion type="bar">Food is yummy!</discussion>
<discussion type="foo">talked about T52 as Discussion 1050</discussion>
</discussions>
Given a particular topic ID ($topicID), I would like to do the following:
-
Devise an XPath expression which is true if there is a
<discussion>withtype="foo"that contains the textT$topicID as Discussion <corresponding-discussion-id>. -
Devise an XPath expression which, given that
$topicID, will extract the textDiscussion <corresponding-discussion-id>.
Is this possible?
Update:
For the first one, I’m thinking I need something like:
exists(
//discussions/discussion[
@type="foo" and
contains(text(), concat($topicId, ??? )) <-- What goes here? I think we need
] some kind of matches(...) around the
) concat(), too.
The following XPath will select the discussions attribute type=”foo” and containing the text “T50 as Discussion” (when $topicid=50).
Given a particular discussion element, the corresponding id is given by:
We can combine the 2 by replacing “.” in the second expression with the entire first expression. Note that if multiple discussions match the inner expression, we will concat their values from the second.
If there were multiple matching discussions, you might process them like this:
Function references:
Personally, I cannot imagine doing serious XSLT development without Michael Kay’s book nearby.