R Nem
R Nem

Reputation: 21

XPATH matches function problem-- works but doesn't work

I'm using Eclipse to run an XSL 2.0 (XPATH 2.0), and I have the following source:

<testTop>
    <Level1 id="abc" Text="from 1-2"/>
    <Level1 id="pqr" Text="from 3-44" />
    <Level1 id="xyz" Text="from 49-101" />
</testTop>

When I test the following expression in Eclipse, //*[matches(@Text, '\d+-\d+')] I get the right nodes, but not the Text attributes themselves

Level1 ID=abc
Level1 ID=pqr
Level1 ID=xyz

... whereas //@Text gives me the Text attributes. Can anyone help me to understand why?? I'd like to get the Text attribute values and parse them using string functions. THE END RESULT SHOULD LOOK LIKE THIS:

<output originalText="from 1-2" value1="1" value2="2" />

Shouldn't I be getting all the attributes that are part of each node that matched?

Upvotes: 2

Views: 1947

Answers (1)

Mads Hansen
Mads Hansen

Reputation: 66723

Your XPath is selecting the elements that have the attributes you want. If you want to select the @Text that match your pattern, you need to adjust your XPath to select the attributes rather than the element.

You could use this XPath:

//*[matches(@Text, '\d+-\d+')]/@Text

or this XPath:

//*/@Text[matches(., '\d+-\d+')]

Upvotes: 1

Related Questions