I have an xml like this:-
<include><method wrap="true"><name>methodA</name></method>...</include>
method node can have wrap attribute with value true or false. absence of attribute should mean it is false.
my predicate is like this:-
//include/method[matches(str, methodA)]
to get the matching nodes.
How can I get the wrap attribute and figure if the attribute is missing?
Try this:
//include/method[#wrap='true'][name = 'methodA']
The test for wrap attribute will only be true if there is an attribute wrap and the value is string true.
Related
For the below path in XPath, I need to have a condition for parser, where the value of the attribute is '1'
./*[local-name()='AccountNumber']/#UndocumentedAccount
I've tried a few things so far, but none seem to work
./*[local-name()='AccountNumber']/#UndocumentedAccount='1'
./*[local-name()='AccountNumber'][#UndocumentedAccount='1']
./*[local-name()='AccountNumber']/#*[UndocumentedAccount and text()='1']
I know how to build such conditions for the value of the element itself, but haven't figured out yet how to do the same for the attribute values
If you are trying to select the UndocumentedAccount attribute only if its value is 1, then the syntax you are probably looking for is:
./*[local-name()='AccountNumber']/#UndocumentedAccount[.='1']
The dot is shorthand for the self axis which is the context item (the item immediately to the left of the [ bracket). You can also try the following to only select AccountNumber nodes having an UndocumentedAccount attribute = 1:
./*[local-name()='AccountNumber' and #UndocumentedAccount ='1']/#UndocumentedAccount
I have a feed here. I'm trying to create an XPath expression that returns items that have a category equal to Bananas. Due to the limitations in my XML parser, I can't use namespaces directly to select items.
The expression /rss/channel/item//*[name()='itunes:category'] returns this:
Element='<itunes:category
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
text="Apples"/>'
Element='<itunes:category
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
text="Bananas"/>'
...
And /rss/channel/item//*[name()='itunes:category']/#text returns this:
Attribute='text=Apples'
Attribute='text=Bananas'
...
But I can't figure out how to limit the response to just a single category (e.g., Bananas)?
I want some kind of expression like this:
/rss/channel/item//*[name()='itunes:category' and contains(., 'Bananas')]
But this doesn't work. It's not syntactically valid. What would be the right XPath expression syntax to just return Bananas?
Do you just mean to filter by attributes of item child, but still return item node?
/rss/channel/item/*[name()='itunes:category' and contains(#text,'Apples')]/parent::item
or simplier
/rss/channel/item[*[name()='itunes:category' and #text='Apples']]
I used Apples in example because using your example xml file there is 0 results for Bananas.
I've below xml and would like to read the value of 'Value' tag whose Name matches 'test2'. I'm using the below xpath , but did not work. Can someone help.
/*[ local-name()='OutputData']/*[ local-name()='OutputDataItem']/*[ local-name()='Name'][normalize-space(.) = 'test2']//*[local-name()='Value']/text()
<get:OutputData>
<get:OutputDataItem>
<get:Name>test1</get:Name>
<get:Value/>
</get:OutputDataItem>
<get:OutputDataItem>
<get:Name>test2</get:Name>
<get:Value>B5B4</get:Value>
</get:OutputDataItem>
<get:OutputDataItem>
<get:Name>test3</get:Name>
<get:Value/>
</get:OutputDataItem>
<get:OutputDataItem>
<get:Name>OP_VCscEncrptCd_VAR</get:Name>
<get:Value/>
</get:OutputDataItem>
</get:OutputData>
Thanks
You were close, but because the get:name and get:value are siblings, you need to adjust your XPath a little.
Your XPath was attempting to address get:value elements that were descendants of get:name, rather than as siblings. Move the criteria that is filtering the get:name into a predicate, then step down into the get:value:
/*[ local-name()='OutputData']/*[ local-name()='OutputDataItem']
[*[ local-name()='Name'][normalize-space(.) = 'test2']]/*[local-name()='Value']/text()
You could also combine the criteria of the predicate filter on the get:name and use an and:
/*[ local-name()='OutputData']/*[ local-name()='OutputDataItem']
[*[ local-name()='Name' and normalize-space(.) = 'test2']]/*[local-name()='Value']/text()
This should work I think:
//*[local-name()="get:Name" and text()="test2"]/following-sibling::*[local-name()="get:Value"]/text()
I have xml with flag:<ns4:flag>false</ns4:flag>. And I want to read this flag and set it's value to FLAG property:
.setProperty( FLAG, xpath("//*[local-name()='flag']/text()", Boolean.class))
Using code above I get 'true' value instead of 'false'. Also tried resultType(Boolean.class) and boolean() xpath function inside expression, but it didn't work out. Any ideas how can I do this cast?
In XPath, the following expression will return boolean value true when the text content equals string value "true", and return boolean value false otherwise :
//*[local-name()='flag']/text() = 'true'
xpathteseter.com demo
So I guess, changing your XPath expression to the above XPath would work.
I want to check in a xml if there is a node with the value "Hotel Hafen Hamburg".
But I get the error.
SimpleXMLElement::xpath(): Invalid predicate on line 25
You can view the xml here.
http://de.sourcepod.com/dkdtrb22-19748
Until now I have written the following code.
$apiUmgebungUrl = "xml.xml";
$xml_umgebung = simplexml_load_file($apiUmgebungUrl);
echo $nameexist = $xml_umgebung->xpath('boolean(//result/name[#Hotel Hafen Hamburg');
It seems that your parantheses and brackets do not close properly at the end of your XPath expression - it should end on ]).
Also, what is Hotel Hafen Hamburg? If it is an attribute called value, your value check should look like this:
[#value="Hotel Hafen Hamburg"]
You cannot just write # and then a value, without specifying where that value is supposed to be.
EDIT: Looking at the Xml document, it seems that Hotel Hafen Hamburg is supposed to be the text content of the <name> element. Therefore, try looking for a text node with that value rather than an attribute:
boolean(//result/name[text() = "Hotel Hafen Hamburg"])