Reputation: 4285
I have some XML that looks like so:
<closure1>
<topClosure>
<ellipsoidalHead>
<standardComponentData>
<variousElements>
<idNumber>234567</idNumber>
<nominalThickness units="in">0.3750</nominalThickness>
</standardComponentData>
</ellipsoidalHead>
</topClosure>
</closure1>
<shell>
<standardComponentData>
<various_elements>
<nominalThickness units="in">0.6250</nominalThickness>
<idNumber>123456</idNumber>
</standardComponentData>
</shell>
<nozzle>
<standardComponentData>
<various_elements>
<attachedToidNumber>123456</attachedToidNumber>
</standardComponentData>
<nozzle>
In my JS code, I already have the <nozzle>
element bomNode
as a jQuery set, i.e.
var bomNode = $("nozzle");
So, for each nozzle element, I need to
Get the value of <attachedToidNumber>
in the <nozzle>
element.
Find the element that contains the <idNumber>
that matches
<attachedToidNumber>
(<shell>
in this case).
<nominalThickess>
element.As you can see, the depth of the desired <idNumber>
element can vary. This is also a very small subset of the whole XML structure, so it can be very large.
I've tried something like this:
var attachedToElement = bomNode.parents().find('idNumber').text() === attachedToId;
but I get false
returned. What's the easiest way to get the desired idNumber
value? I'm sure it's something simple, but I'm just missing it.
Thanks.
UPDATE: I realized that bomNode
is at the top level, I don't need to go up. a level. Doing something like this
var attachedToElement = bomNode.parents().siblings().find('idNumber')
gives me a list of children elements that have an <idNumber>
element. So, I need to find the one that has the desired value. My thought is to use .each()
. However, that value is defined outside of the .each()
function, so I don't have anything to match against. Once I have the list of matches, what's the easiest way to get the set that has the <idNumber>
value I want?
Upvotes: 0
Views: 82
Reputation: 11000
You were right - you missed a simple thing:
shell
is not a parent of nozzle
. They are siblings
. Try this:
var attachedToElement = bomNode.siblings().find('idNumber').text() === attachedToId;
But this would return true (if true) - not the actual value.
Upvotes: 1