简体   繁体   English

基于Java中的条件的xpath

[英]xpath based on condition in java

I have an xml of the following structure: 我有以下结构的xml:

<Root>
  <Sample>
    <Materil material_class="book" />
    <Book Name="harry" Price="8" />
    <Book Name="small things" Price="9" />
    <Book Name="snow" Price="10" />
  </Sample>
 <Commodity>
    <Sample>
        <Materil material_class="sub" />
        <Book Name="sherin" Price="8" />
        <Book Name="bigthings" Price="9" />
        <Book Name="leopard" Price="10" />
    </Sample>
    <Commodity>
         <Sample>
            <Materil material_class="sub" />
            <Book Name="azxcv" Price="86" />
            <Book Name="ddddd" Price="79" />
            <Book Name="qwert" Price="810" />
         </Sample>
    </Commodity>
    <Commodity>
         <Sample>
            <Materil material_class="subtwo" />
            <Book Name="ratnam" Price="86" />
            <Book Name="shantharam" Price="99" />
            <Book Name="da vinci" Price="10" />
         </Sample>
    </Commodity>
 </Commodity>
</Root>

Is there a way to iterate this xml based on condition like,if the material_class = "sub" , iterate the Book tag below that and store the @Name and @Price . 有没有一种方法可以根据条件来迭代此xml,例如,如果material_class = "sub" ,则可以迭代下面的Book标记并存储@Name@Price If the material_class = "book" , iterate the Book tag below that. 如果material_class = "book" ,则Book下面迭代Book标签。 Also i want to get the length of number of /Root/Commodity/Commodity tags (in this case , it is two). 我也想得到/Root/Commodity/Commodity标签数量的长度(在这种情况下,是两个)。 Any help is appreciated. 任何帮助表示赞赏。 I am new to XPath. 我是XPath的新手。

To get the Book @Name and @Price for @material_class='sub' or @material_class='book' , use this XPATH 要获取@material_class='sub'@material_class='book'的书@Name@Price ,请使用此XPATH

/Root[descendant-or-self::Sample or Sample[descendant-or-self::*]]//*[Materil[@material_class='sub' or @material_class='book']]/Book

OR 要么

//Sample[Materil[@material_class='book' or @material_class='sub']]/Book

After loading this XPATH, to print the Name use @Name and Price use @Price 加载此XPATH后,要打印名称,请使用@Name和Price,请使用@Price

To get the length of number of /Root/Commodity/Commodity tags, XPATH is 要获取/ Root / Commodity / Commodity标签的数量,XPATH为

/Root/Commodity/Commodity

OR 要么

//Commodity/Commodity

JAVA: JAVA:

NodeList node = (NodeList) xpath.evaluate("/Root/Commodity/Commodity", xml, XPathConstants.NODESET);
count = node.getLength(); // OUTPUTS: 2

For your information, actually rendering XML through XSLT is much better performance and easier to implement. 就您的信息而言,实际上通过XSLT呈现XML具有更好的性能并且更易于实现。

<xsl:apply-templates select="//Sample[Materil[@material_class='sub']]/Book"/>
<xsl:apply-templates select="//Sample[Materil[@material_class='book']]/Book"/>

I do not have access to an XSL editor right now, so have not tested the above, but it will give you an idea. 我现在无法访问XSL编辑器,因此尚未测试以上内容,但是它将为您提供一个思路。 Select the Book, and constrain the Materil with the condition you want. 选择“书”,并以所需的条件约束Materil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM