需要有关Xpath Expression的帮助

时间:2014-04-24 05:22:47

标签: xml xpath

需要一些关于Xpath表达式的帮助。我有以下xml:

<ldaConfigTblV4_0>
<Value>
  <LDA_CONFIG_T_V4>
    <CONFIG_ATTRB_VALUE>90</CONFIG_ATTRB_VALUE>
    <BOX_LABEL>
      <Value>   
        <TIEGROUP>1</TIEGROUP>
        <LABEL_TEMPLATE>LABEL_TEMPLATE</LABEL_TEMPLATE>
        <GTINTYPE>TEST GTINTYPE</GTINTYPE>
        <GTINVALUE>1</GTINVALUE>
        <MODELNUM>TEST MODELNUM</MODELNUM>
        <MODELDESCRIPTION>TEST MODELDESC</MODELDESCRIPTION>
        <COLOR>TEST Color</COLOR>
        <COUNTRY>TEST COUNTRY</COUNTRY>       
      </Value>        
    </BOX_LABEL>
    <TIEGROUP>1</TIEGROUP>
    <TIEGROUPIsNull>false</TIEGROUPIsNull>
    <CONFIG_ATTRB_NAME>LOB</CONFIG_ATTRB_NAME>
    <WO_ID>20015670070</WO_ID>
    <WO_IDIsNull>false</WO_IDIsNull>
    <WORK_ORDER_LINE_NO>1</WORK_ORDER_LINE_NO>
    <WORK_ORDER_LINE_NOIsNull>false</WORK_ORDER_LINE_NOIsNull>
    <SYSTEM_LABEL>
      <Value>
        <TIEGROUP>1</TIEGROUP>
        <LABEL_TEMPLATE>LABEL_TEMPLATE</LABEL_TEMPLATE>
        <GTINTYPE>TEST GTINTYPE</GTINTYPE>
        <GTINVALUE />
        <MODELNUM>TEST MODELNUM</MODELNUM>
        <MODELDESCRIPTION>TEST MODELDESC</MODELDESCRIPTION>
        <COLOR>TEST Color</COLOR>
        <COUNTRY>TEST COUNTRY</COUNTRY>           
      </Value>        
    </SYSTEM_LABEL>
    <SALES_ORDER_REF>10016041740</SALES_ORDER_REF>
    <SALES_ORDER_REFIsNull>false</SALES_ORDER_REFIsNull>
  </LDA_CONFIG_T_V4>
  <LDA_CONFIG_T_V4>
    <CONFIG_ATTRB_VALUE>91</CONFIG_ATTRB_VALUE>
    <BOX_LABEL>
      <Value>
        <TIEGROUP>2</TIEGROUP>
        <LABEL_TEMPLATE>LABEL_TEMPLATE_2</LABEL_TEMPLATE>
        <GTINTYPE>TEST GTINTYPE_2</GTINTYPE>
         <GTINVALUE>2</GTINVALUE>
        <MODELNUM>TEST MODELNUM_2</MODELNUM>
        <MODELDESCRIPTION>TEST MODELDESC_2</MODELDESCRIPTION>
        <COLOR>TEST Color_2</COLOR>
        <COUNTRY>TEST COUNTRY_2</COUNTRY>
      </Value>        
    </BOX_LABEL>
    <TIEGROUP>2</TIEGROUP>
    <TIEGROUPIsNull>false</TIEGROUPIsNull>
    <CONFIG_ATTRB_NAME>LOB</CONFIG_ATTRB_NAME>
    <WO_ID>20015670070</WO_ID>
    <WO_IDIsNull>false</WO_IDIsNull>
    <WORK_ORDER_LINE_NO>2</WORK_ORDER_LINE_NO>
    <WORK_ORDER_LINE_NOIsNull>false</WORK_ORDER_LINE_NOIsNull>
    <SYSTEM_LABEL>
      <Value>
        <TIEGROUP>2</TIEGROUP>
        <LABEL_TEMPLATE>LABEL_TEMPLATE_2</LABEL_TEMPLATE>
        <GTINTYPE>TEST GTINTYPE_2</GTINTYPE>
        <GTINVALUE />
        <MODELNUM>TEST MODELNUM_2</MODELNUM>
        <MODELDESCRIPTION>TEST MODELDESC_2</MODELDESCRIPTION>
        <COLOR>TEST Color_2</COLOR>
        <COUNTRY>TEST COUNTRY_2</COUNTRY>
      </Value>        
    </SYSTEM_LABEL>
    <SALES_ORDER_REF>10016041740</SALES_ORDER_REF>
    <SALES_ORDER_REFIsNull>false</SALES_ORDER_REFIsNull>
  </LDA_CONFIG_T_V4>
  <LDA_CONFIG_T_V4>
    <CONFIG_ATTRB_VALUE>90</CONFIG_ATTRB_VALUE>
    <BOX_LABEL>
      <Value>   
        <TIEGROUP>1</TIEGROUP>
        <LABEL_TEMPLATE>LABEL_TEMPLATE</LABEL_TEMPLATE>
        <GTINTYPE>TEST GTINTYPE</GTINTYPE>
        <GTINVALUE>1</GTINVALUE>
        <MODELNUM>TEST MODELNUM</MODELNUM>
        <MODELDESCRIPTION>TEST MODELDESC</MODELDESCRIPTION>
        <COLOR>TEST Color</COLOR>
        <COUNTRY>TEST COUNTRY</COUNTRY>       
      </Value>        
    </BOX_LABEL>
    <TIEGROUP>1</TIEGROUP>
    <TIEGROUPIsNull>false</TIEGROUPIsNull>
    <CONFIG_ATTRB_NAME>LOB</CONFIG_ATTRB_NAME>
    <WO_ID>20015670070</WO_ID>
    <WO_IDIsNull>false</WO_IDIsNull>
    <WORK_ORDER_LINE_NO>1</WORK_ORDER_LINE_NO>
    <WORK_ORDER_LINE_NOIsNull>false</WORK_ORDER_LINE_NOIsNull>
    <SYSTEM_LABEL>
      <Value>
        <TIEGROUP>1</TIEGROUP>
        <LABEL_TEMPLATE>LABEL_TEMPLATE</LABEL_TEMPLATE>
        <GTINTYPE>TEST GTINTYPE</GTINTYPE>
        <GTINVALUE />
        <MODELNUM>TEST MODELNUM</MODELNUM>
        <MODELDESCRIPTION>TEST MODELDESC</MODELDESCRIPTION>
        <COLOR>TEST Color</COLOR>
        <COUNTRY>TEST COUNTRY</COUNTRY>           
      </Value>        
    </SYSTEM_LABEL>
    <SALES_ORDER_REF>10016041740</SALES_ORDER_REF>
    <SALES_ORDER_REFIsNull>false</SALES_ORDER_REFIsNull>
  </LDA_CONFIG_T_V4>
</Value>
</ldaConfigTblV4_0>

我希望获得具有不同TIEGROUP的LDA_CONFIG_T_V4个节点,在这种情况下,我应该只获得前两个LDA_CONFIG_T_V4节点,因为最后一个节点与第一个节点具有相同的TIEGROUP。

我尝试使用以下XPath表达式

ldaConfigTblV4_0/Value/LDA_CONFIG_T_V4[TIEGROUP[not(.=preceding::TIEGROUP)]]

但是当我有多个LDA_CONFIG_T_V4节点时,它不起作用,如果我有单个LDA_CONFIG_T_V4节点,它可以正常工作。我也试过ldaConfigTblV4_0/Value/LDA_CONFIG_T_V4[TIEGROUP[not(.=following::TIEGROUP)]],但没有用。

1 个答案:

答案 0 :(得分:1)

你非常接近。您的想法是正确的,但您的not()位置不正确,无法实现您的目标。

ldaConfigTblV4_0/Value/LDA_CONFIG_T_V4[not(TIEGROUP = preceding::TIEGROUP)]

以下效率更高一些(使用prececing-sibling代替preceding会导致检查的节点更少:

ldaConfigTblV4_0/Value/LDA_CONFIG_T_V4[not(TIEGROUP = preceding-sibling::LDA_CONFIG_T_V4/TIEGROUP)]