Home > front end >  Problem reading XML with Nokogiri using xpath with round brackets
Problem reading XML with Nokogiri using xpath with round brackets

Time:07-09

I am currently having a problem with Nokogiri XML Parsing.

XML Example:

<casting>
<CASTPARTICIPANT>
<person>
<PERSON fullname="John" name="Wick" firstname="John Wick">
<pe_functions/>
</PERSON>
</person>
<function>
<FUNCTION function="Productor"/>
</function>
</CASTPARTICIPANT>
<CASTPARTICIPANT>
<person>
<PERSON fullname="John Wade" name="Wade" firstname="John">
</PERSON>
</person>
<function>
<FUNCTION function="Actor / Principal"/>
</function>
</CASTPARTICIPANT>
<CASTPARTICIPANT>
<person>
<PERSON fullname="Benjamin Franklyn" name="Franklyn" firstname="Benjamin">
</PERSON>
</person>
<function>
<FUNCTION function="Realizer"/>
</function>
</CASTPARTICIPANT>
</casting>

When doing doc.xpath("//@name[../../../function/FUNCTION/starts-with(@function,'Actor')]")

I am getting the error: Invalid expression: //@name[../../../function/FUNCTION/starts-with(@function,'Actor')]

The expression is working on http://xpather.com/

When doing doc.xpath("//@name[../../../function/FUNCTION/@function]") it is working, I also tested doc.xpath("//@name[../../../function/FUNCTION/(@function)]") and it works on http://xpather.com/ but showing the same error with Nokogiri "Invalid expression"

Which make me think is a problem with the round brackets.

Any idea? Thanks

CodePudding user response:

I'm not sure why it works on xpather.com

I think the correct expression should be:

doc.xpath("//@name[../../../function/FUNCTION[starts_with(@function,'Actor')]]")

(note: my NOKOGIRI doesn't support starts_with, but the error message clearly states that's the problem, I substituted contains for testing)

CodePudding user response:

The expression is valid in XPath 2.0 but not in 1.0, so it's a version problem. XPath 1.0 doesn't allow a function call on the rhs of the "/" operator.

You haven't actually said what you are trying to achieve. The XPath 2.0 expression, while valid, returns a sequence of booleans, one for each selected @name attribute, which doesn't seem very useful. It's more likely that you want to retrieve the PERSON elements that satisfy some condition - but at this point we're trying to guess your requirements.

  • Related