I have a node on an XML file for which I need to select. To select, I have to look at one of the element's value. Once selected, I will then arrange for the entire node to be deleted from the file.
We have a framework that deals with the selecting of nodes however I currently receive the following error: Expression must evaluate to a node-set.
All the framework does is takes in the XMLElement and XPath, and uses what looks to be a defined function named .selectNode(xPath) to find the node. This is what throws the above error.
My XML Data set(which is the XMLElement) looks like this:
<topParent value1="a" value2 = "b">
<att1 value3="c" value4 = "d">
<myline data="e" moredata="f" />
<myline data="g" moredata="h" />
</att1>
</topParent>
and my xPath for selecting looks like : //myline/[#moredata='h']
The idea is to select "myline" node when "moreData" equals h
I've only come across XPath in the last few hours but I can't see why this node isn't being selected.
You shouldn't put a slash before a predicate. It's wrong syntax.
Just use
//myline[#moredata='h']
to select all the myline elements which satisfy the condition.
Related
I have the following XML structure
<Root>
<BundleItem>
<Item>1</Item>
<Item>2</Item>
<Item>3</Item>
</BundleItem>
<Item>4</Item>
<Item>5</Item>
<Item>6</Item>
<BundleItem>
<Item>7</Item>
<Item>8</Item>
<Item>9</Item>
</BundleItem>
</Root>
And by providing the following xPath
//Item[1]
I am selecting
<Item>1</Item>
<Item>4</Item>
<Item>7</Item>
My goal is to select only <Item>1</Item> or <Item>7</Item> regardless of the parent element where they are found and only depending on the position, which i am providing in the xPath.
Is it possible to do that only by using the position and without providing additional criterias in the xPath ?
//Item[1] selects the all the first child elements that are <Item/> regardless of their parent.
To get the two items you are looking for you could use //Item[text() = 1 or text() = 7].
A good tutorial can be found at w3schools.com and you can play with XPath expressions over your XML input here. (I am not affiliated with either of these resources but find them useful.)
In one of the API solution, incoming request is in XML format, and i need to fetch first child node tag name to make decision to run the logic. I am using xpath to get the tag name, when in am running xpath i am getting error "Can not convert #STRING to a NodeList"
I have tried with local-name and name, but both are giving same error.
my xml is as below
<p:Check xmlns:p="http://amarwayx.com.cu/WCSXMLSchema/creptonium">
<AttributeChnageLocal>
<TaskID>17723</TaskID>
<BatchID>12345</BatchID>
<Expiry>2022-12-06</Expiry>
<TimeStamp>2019-07-20T22:45:48</TimeStamp>
</AttributeChnageLocal>
</p:Check>
and Xpath i used are
local-name(/p:Check/*)
name(/p:Check/*)
local-name(/p:Check/*[1])
name(/p:Check/*[1])
how ever is some online xpath evaluator has evaluated correct name(AttributeChnageLocal), i am not getting where the xpath syntax is wrong.
below is my tool snapshot.
same kind of expression works fine
You have ticked a box labelled "store the string value of the selected node as text", which suggests that the XPath evaluation tool you are using expects your XPath expression to select a node; but it doesn't, it selects a string.
I don't know what this tool you are using is, but unfortunately all its options seem to assume that you are selecting nodes.
I was selecting all attributes id and everything was going nicely then one day requirements changed and now I have to select all except one!
Given the following example:
<root>
<structs id="123">
<struct>
<comp>
<data id="asd"/>
</comp>
</struct>
</structs>
</root>
I want to select all attributes id except the one at /root/structs/struct/comp/data
Please note that the Xml could be different.
Meaning, what I really want is: given any Xml tree, I want to select all attributes id except the one on element /root/structs/struct/comp/data
I tried the following:
//#id[not(ancestor::struct)] It kinda worked but I want to provide a full xpath to the ancestor axis which I couldn't
//#id[not(contains(name(), 'data'))] It didn't work because name selector returns the name of the underlying node which is the attribute not its parent element
The following should achieve what you're describing:
//#id[not(parent::data/parent::comp/parent::struct/parent::structs/parent::root)]
As you can see, it simply checks from bottom to top whether the id attribute's parent matches the path root/structs/struct/comp/data.
I think this should be sufficient for your needs, but it does not 100% ensure that the parent is at the path /root/structs/struct/comp/data because it could be, for example, at the path /someOtherHigherRoot/root/structs/struct/comp/data. I'm guessing that's not a possible scenario in your XML structure, but if you had to check for that, you could do this:
//#id[not(parent::data/parent::comp/parent::struct/parent::structs/parent::root[not(parent::*)])]
I am using OSB and have one long XML containing parent and child node.
This is my XML
<XML>
<RefreshLaborApproval2RSP xmlns:XPathFunction="http://www.oracle.com/XSL/Transform/java/mol.prc.labourapproval.XPathFunction" xmlns:tsd="http://namespaces.softwareag.com/tamino/TaminoSchemaDefinition">
<ERRORCODE>
<VALUE>dfdfdf</VALUE>
</ERRORCODE>
<LABORRECORDS>
<LABORNAT>
<VALUE>569</VALUE>
</LABORNAT>
</LABORRECORDS>
<LABORRECORDS>
<LABORNAT>
<VALUE>218</VALUE>
</LABORNAT>
</LABORRECORDS>
</RefreshLaborApproval2RSP>
</XML>
When I use expression XML/* its gives me the whole XML from <RefreshLaborApproval2RSP> to </RefreshLaborApproval2RSP> and kept the same in one variable called xmlparentNode.
And then when I use $xmlparentNode/LABORRECORDS then I get only istLABORRECORDS child element like below.
<LABORRECORDS>
<LABORNAT>
<VALUE>569</VALUE>
</LABORNAT>
</LABORRECORDS>
But I want all <LABORRECORDS> child elements.
I dont know what exacly you want achieve, but you can:
Assign variable xmlparentNode,
Use For Each for example:
a) For Each Variable: Laborrecords
b) XPath: ./LABORRECORDS
c) In Variable: xmlparentNode
d) Index Variable: index
e) Count Variable: count
And then inside For Each do Assign expression: $Laborrecords in variable to get first value, then second.
Do some inserts or something with it.
I do not know if this will solve your problem.
The XML expression that you are using is right.
If the XML elements are very long and you are using variable window (during debugging) to see the element returned, there could be a possible chance to data loss.
Edit: I think I found the answer but I'll leave the open for a bit to see if someone has a correction/improvement.
I'm using xpath in Talend's etl tool. I have xml like this:
<root>
<employee>
<benefits>
<benefit>
<benefitname>CDE</benefitname>
<benefit_start>2/3/2004</benefit_start>
</benefit>
<benefit>
<benefitname>ABC</benefitname>
<benefit_start>1/1/2001</benefit_start>
</benefit>
</benefits>
<dependent>
<benefits>
<benefit>
<benefitname>ABC</benefitname>
</benefit>
</dependent>
When parsing benefits for dependents, I want to get elements present in the employee's
benefit element. So in the example above, I want to get 1/1/2001 for the dependent's
start date. I want 1/1/2001, not 2/3/2004, because the dependent's benefit has benefitname ABC, matching the employee's benefit with the same benefitname.
What xpath, relative to /root/employee/dependent/benefits/benefit, will yield the value of
benefit_start for the benefit under parent employee that has the same benefit name as the
dependent benefit name? (Note I don't know ahead of time what the literal value will be, I can't just look for 'ABC', I have to match whatever value is in the dependent's benefitname element.
I'm trying:
../../../benefits/benefit[benefitname=??what??]/benefit_start
I don't know how to refer to the current node's ancestor in the middle of
the xpath (since I think "." at the point I have ??what?? will refer to
the benefit node of the employee/benefits.
EDIT: I think what I want is "current()/benefitname" where the ??what?? is. Seems to work with saxon, I haven't tried it in the etl tool yet.
Your XML is malformed, and I don't think you've described your siduation very well (the XPath you're trying has a bunch of ../../s at the beginning, but you haven't said what the context node is, whether you're iterating through certain nodes, or what.
Supposing the current context node were an employee element, you could select benefit_starts that match dependent benefits with
benefits/benefit[benefitname = ../../dependent/benefits/benefit/benefitname]
/benefit_start
If the current context node is a benefit element in a dependents section, and you want to get the corresponding benefit_start for just the current benefit element, you can do:
../../../benefits/benefit[benefitname = current()/benefitname]/benefit_start
Which is what I think you've already discovered.