I have a requirement to sum amount from every element i have in my response using xpath
,however condition is i am not sure about how many tags I am going to get in my response.
Sum= amount1*value1+ amount2*value2+amount3*value3+....
<root>
<element>
<amount>10</amount>
<value>2</value>
</element>
<element>
<amount>20</amount>
<value>2</value>
</element>
<element>
<amount>30</amount>
<value>2</value>
</element>
</root>
can some one please help?
You can try below XPath to get summ of all amount nodes:
sum(//element/amount)
Considering updated question:
sum(//element/sum(./amount * ./value))
Related
I have below xml data:
Control:
<Data>
<propertyValues>
<propertyName>Name1</propertyName>
<value>
<text>
<value>Value1</value>
</text>
</value>
</propertyValues>
<propertyValues>
<propertyName>Name2</propertyName>
<value>
<text>
<value>Value2</value>
</text>
</value>
</propertyValues>
</Data>
Test:
<Data>
<propertyValues>
<propertyName>Name2</propertyName>
<value>
<text>
<value>Value2</value>
</text>
</value>
</propertyValues>
<propertyValues>
<propertyName>Name1</propertyName>
<value>
<text>
<value>Value1</value>
</text>
</value>
</propertyValues>
</Data>
And I would expect these 2 documents are "same".
How can I config xmlUnit to make it work? (I'm using xmlunit 2.6.3)
Thanks
Leon
This is pretty similar to the running example of the "Selecting Nodes" part of XMLUnit's User Guide.
You need to use an ElementSelector that picks the correct propertyValues element when looking at the list of elements and then decides to compare the elements that contain the same nested text inside the only child element named propertyName. This directly translates into
ElementSelectors.conditionalBuilder()
.whenElementIsNamed("propertyValues")
.thenUse(ElementSelectors.byXPath("./propertyName", ElementSelectors.byNameAndText))
...
and then you need to add whatever other rules are required to make the rest work. Looking at the visible rest of your example there are no ambiguous children and a simple
...
.elseUse(ElementSelectors.byName)
.build();
will do.
<Sections>
<Classes>
<Class>
<ClassStd>VI</ClassStd>
<ClassName>XYZ</ClassName>
</Class>
<Class>
<ClassStd>VII</ClassStd>
<ClassName>ABC</ClassName>
</Class>
</Classes>
<Classes>
<Class>
<ClassStd>VIII</ClassStd>
<ClassName>EFG</ClassName>
</Class>
<Class>
<ClassStd>IX</ClassStd>
<ClassName>MNO</ClassName>
</Class>
</Classes>
</Sections>
I want to get the ClassName values (XYZ,ABC,EfG,MNO) using Xpath. I tried using
//Sections/Classes/Class/*/ClassName/text() and other Xpath queries but i'm not getting desired results. I want to loop through every Classes and Every Class and get the ClassName values. Since the number of Classes or Class is fixed i have to loop till the end to get Values. How i can construct such a loop in Xpath ?
You need to change your xpath to:
//sections/classes/classname/text()
Taking this xml piece as example:
<list>
<element>
<title>Element 1</title>
<group>Group 1</group>
</element>
<element>
<title>Element 2</title>
<group>Group 1</group>
</element>
<element>
<title>Element 3</title>
<group>Group 1</group>
<group>Group 2</group>
</element>
<element>
<title>Element 4</title>
<group>Group 2</group>
<group>Group 3</group>
</element>
</list>
To get all groups I use the following xpath:
//group/text()
and it works fine -I remove duplicates later in python using a set as I don't know if I can do it with xpath-. But When I want to get the elements that contain "Group 3" I try the following xpath:
//element[contains(group/text(), "Group 3")]
and I get an empty result. While when I search elements that contain "Group 1" with:
//element[contains(group/text(), "Group 1")]
I get the correct result with 3 elements. And if I look for "Group 2" I get a wrong result with only one element.
What I'm not taking into account? How can I make those searches by group?
contains can only test, if a string occurs in another string. group/text() is not a string, it is a set/sequence of nodes.
You can use
//element[group/text() = "Group 1"]
<Item id="item0">
<Links>
<FirstLink id="link1" target="one"/>
<SecondLink id="link2" target="two"/>
</Links>
<Data>
<String>content</String>
</Data>
</Item>
<Item id="item1">
<Links>
<FirstLink id="link1" target="two"/>
<SecondLink id="link2" target="two"/>
</Links>
<Data>
<String>content</String>
</Data>
</Item>
I have created a Nokogiri-NodeSet with this structure, i.e. a list of items with links and data children.
How can I filter any items that don't match a certain value in the 'target'-attribute of <FirstLink>?
Actually, what I want in the end is to extract the <Data><String>-Content of every <Item> that matches a certain value in it's <FirstLink> "Target"-Attribute.
I've tried several approaches already but I'm at a loss as to how to identify an element by an attribute of it's grandchild, then extracting the content of this grandchild's parent's sibling, X(.
We can build up an XPath expression to do this. Assuming we are starting from the whole XML document, rather than the node-set you already have, something like
//Item
will select all <Item> elements (I’m guessing you already have something like that to get this node-set).
Next, to select only those <Item> elements which have <Links><FirstLink> where FirstLink has a target attribute value of one:
//Item[Links/FirstLink[#target='one']]
and finally to select the Data/String children of those nodes:
//Item[Links/FirstLink[#target='one']]/Data/String
So with Nokogiri you could use something like this (where doc is your parsed document):
doc.xpath("//Item[Links/FirstLink[#target='one']]/Data/String")
or if you want to use the node-set you already have you can use a relative expression:
nodeset.xpath("self::Item[Links/FirstLink[#target='one']]/Data/String")
I completely didn't understand what your goal is. But using a guess, I am trying to show you, how to proceed in this case :
require 'nokogiri'
doc = Nokogiri::XML <<-xml
<Item id="item0">
<Links>
<FirstLink id="link1" target="one"/>
<SecondLink id="link2" target="two"/>
</Links>
<Data>
<String>content1</String>
</Data>
</Item>
<Item id="item1">
<Links>
<FirstLink id="link1" target="two"/>
<SecondLink id="link2" target="two"/>
</Links>
<Data>
<String>content2</String>
</Data>
</Item>
xml
#xpath method with the expression "//Item", will select all the Item nodes. Then those Item nodes will be passed to the #reject method to select only those nodes, that has a node called Links having the target attribute value is "one". If any of the links, either FirstLink or SecondLink has the target attribute value "one", for that nodes grandparent node Item will be selected.
node.at("//Links/FirstLink")['target'] will give you the string say "one" which is a value of target attribute of the node, FirstLink of first Item nodes , then "two" from the second Item node. The part ['any vaue'] in node.at("//Links/FirstLink")['target']['any vaue'] is a call to the String#[] method.
Remember below approach will give you the flexibility of the use regular expression too.
nodeset = doc.xpath("//Item").reject do |node|
node.at("//Links/FirstLink")['target']['any vaue']
end
Now nodeset contains only the required Item nodes. Now I use #map, passing each item node inside it to collect the content of the String node. Then #at method with an expression //Data/String, will select the String node. Then #text, will give you the content of each String node.
nodeset.map { |n| n.at('//Data/String').text } # => ["content1"]
How to write a Xpath for two attributes? e.g. i need to get a value of discount > 20% and also the same discount is greater than amount 200(without any link to base value)
You can combine constraints in predicates. E.g.:
from lxml import etree
doc = etree.XML("""<xml>
<items>
<item discount_perc="25" discount_value="250">Something</item>
</items>
</xml>
""")
doc.xpath('items/item[#discount_perc > 20 and #discount_value > 200]')
Will try to answer by a simple example. Imagine you have the following xml:
<?xml version="1.0"?>
<data>
<node value="10" weight="1">foo</node>
<node value="10" weight="2">bar</node>
</data>
Then use this query to select the first <node>'s text:
//node[#value="10" and #weight="1"]/text()
and this for the second:
//node[#value="10" and #weight="2"]/text()
Hope this helps.