Toshe
Toshe

Reputation: 786

Alternative Syntax for an XPath in nokogiri, parsing an ODF

I have a pretty straightforward ODF document that I want to parse with nokogiri.

The XML looks like this:

<office:body>
<office:text>
    <text:sequence-decls>
        ...
    </text:sequence-decls>
    <text:section text:name="categorylevel1" text:style-name="Sect1">
        <text:p text:style-name="Title">[CATEGORY_LEVEL_1_NAME]</text:p>
    </text:section>
</office:text>

Currently I'm trying to select the categorylevel1 section of the document. The library I'm using generates the XPath automatically and yields
".//text:section[@text:name='categorylevel1']"
which is correct. Now the problem is the library (nokogiri) seems to accept this path under MRI but not under JRuby (throws a SyntaxError). Apparently the Java version of the library does not support namespace attributes.

Is there an alternative way to reference the section of the document? For example just by using the text:name attribute? Is there a way to ignore the namespace? The section's text:name attribute value will be unique for the whole document so miss-referencing would not be an issue.

Upvotes: 0

Views: 251

Answers (1)

har07
har07

Reputation: 89285

"Is there an alternative way to reference the section of the document? For example just by using the text:name attribute?"

You can ignore element name by using *, this way you can reference the element just by using the text:name attribute :

.//*[@text:name='categorylevel1']

"Is there a way to ignore the namespace?"

You can use local-name() to ignore namespaces. For example, to filter element by text:name attribute value while ignoring the attribute's namespace :

.//*[@*[local-name()='name' and .='categorylevel1']]

Upvotes: 2

Related Questions