Inetquestion
Inetquestion

Reputation: 185

xmlstarlet does not work with end of string anchor $

Using the xpath statement shown with xmlstarlet, it does not honor the end of string anchor, and selects both 'aaa' and 'bbb'. How can do I structure the xpath statement to match on an attribute which ends with 998?

Xpath statement:

-v "../Property[contains(@Name, '998$')]

File object.xml:

<Object>
    <Property Name="1230-02324998">
        <Value>aaa</Value>
    </Property>
    <Property Name="3223-99824993">
        <Value>bbb</Value>
    </Property>
</Object>

Upvotes: 0

Views: 565

Answers (2)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

Tried this: xmlstarlet sel -B -t -m "//Object" -v "..Property[substring(@Name, string-length(@Name) - 2) = '998']" The answer should be: "aaa", but it didn't work.

Please be careful when typing answers to your questions into the shell. Martin Honnen did not tell you to start the expression with .., which makes it an invalid expression.

Given that your XML input is stored in an XML document called "object.xml":

$ xml sel -B -t -m "Object" -v "Property[substring(@Name, string-length(@Name) - 2) = '998']" object.xml
aaa

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167571

The contains function does not have any regular expression support. Use Property[substring(@Name, string-length(@Name) - 2) = '998'].

Upvotes: 1

Related Questions