开发者

Need to extract XML node using jquery, where node's sibling equals a certain value; using xPath now

Here is a snippet of the XML file:

<Catalog>
  <CDs>
    <CD>
      <Manuals>
        <Manual>123DHX<Manual>
        <Manual>5axDHX<Manual>
        <Manual>5DaDHX<Manual>
        <Manual>5dADHX<Manual>
        <Manual>10XDHX<Manual>
      </Manuals>
      <SomeOtherNode>text</SomeOtherNode>
      <YetAnotherNode><text</YetAnotherNode>
      <Blueprint>No.8</Blueprint>
    </CD>
  </CDs>  
</Catalog>

Currently, I'm using xPath to select the Manuals node, where the Blueprint node (it's sibling) equals the value in a select/dropdown box.

xmlobject.selectSingleNode("/Catalog/CDs/CD/Manuals[../Blueprint='" + document.getElementById("Blueprint_Select").value + "']");

How can I do the equivalent in jQuery?

I tried the follwoing but it selects the Blueprint node, not its sibling, the Manuals node, which is what I want:

$(xmlobject).find("Blueprint:contains(" + $("#Blueprint_Select").val() + ")");

I found this link, jQuery to get matching nodes开发者_如何转开发 in XML, but it didn't help much, since .has() is for descendents, and I can't find an similar syntax in jquery for ascendents or siblings.

Thank you in advance for any help.


If the <Manuals> element is always the immediate sibling of the <Blueprint> element, you can use the prev() method:

$(xmlobject).find("Blueprint:contains(" + $("#Blueprint_Select").val() + ")")
            .prev("Manuals");

EDIT: If you cannot predict the order of the elements, use the siblings() method:

$(xmlobject).find("Blueprint:contains(" + $("#Blueprint_Select").val() + ")")
            .siblings("Manuals");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜