I would like to search across multiple XML files for nodes with an optional attribute. The files missing the attribute I am looking for do not declare the namespace it belongs. I am searching using a simple XPath as in the following example:
Here I am interested in the other_attribute of node:
<?xml version="1.0" encoding="UTF-8"?>
<otherfile xmlns:xs="something" xmlns:optional="something_else">
<node attribute = "hohoho" optional:other_attribute= "mary Xmass">
</node>
</otherfile>
And I am matching it using the XPath //@optional:other_attribute. However, when trying to do the same in the following file:
<?xml version="1.0" encoding="UTF-8"?>
<file xmlns:xs="something">
<node attribute = "hohoho">
</node>
</file>
The search fails because the namespace optional is not declared in the second example file. Is there a way to do a conditional search for the attribute with using the Xpath syntax?
@optional:other_attributeor with@other_attribute? Or you want to fetch node with either@optional:other_attributeor@attribute? Can you clarify? It's not clear from description.@optional:other_attributeonly if it exists. My problem is that the Xpath query fails if the namespace is not declared instead of just returning nothing (node does not exist)