如何在数组下为数组定义XPATH

时间:2017-09-05 17:04:11

标签: arrays xml parsing xpath xml-parsing

我有一个XML文件如下所示,我在数组下有一个数组。 这里我只有2条记录

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns0:executeSavedQueryResponse xmlns:ns0="http://xmlns.xyz.com/abcdobjects/Core/Search/V1">
         <response>
            <messageId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <messageName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <statusCode>SUCCESS</statusCode>
            <responses>
               <results>
                  <tableIdentifier>
                     <classId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <className xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <objectId>6152767</objectId>
                     <objectName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <tableId>-102</tableId>
                     <tableName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <tableDisplayName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  </tableIdentifier>
                  <row rowId="1">
                     <objectReferentId>
                        <classId>2468022</classId>
                        <className>BondWire</className>
                        <classDisplayName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        <objectId>6198569</objectId>
                        <objectName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        <objectVersion xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        <version xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     </objectReferentId>
                     <additionalRowInfo xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <number attributeId="1001" xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">DELTA2</number>
                     <productLineS attributeId="1004" xsi:type="common:abcdListEntryType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:common="http://xmlns.xyz.com/abcdobjects/Core/Common/V1">
                        <listName xsi:nil="true"/>
                        <selection>
                           <id>2580243</id>
                           <apiName>BROADBAND_ACCESS</apiName>
                           <value>Broadband Access</value>
                        </selection>
                        <selection>
                           <id>2580244</id>
                           <apiName>BROADBAND_MEDIA</apiName>
                           <value>Broadband Media</value>
                        </selection>
                     </productLineS>
                  </row>
                  <row rowId="2">
                     <objectReferentId>
                        <classId>2484539</classId>
                        <className>Mould</className>
                        <classDisplayName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        <objectId>6198572</objectId>
                        <objectName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        <objectVersion xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                        <version xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     </objectReferentId>
                     <additionalRowInfo xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <number attributeId="1001" xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">DELTA3</number>
                     <productLineS attributeId="1004" xsi:type="common:abcdListEntryType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:common="http://xmlns.xyz.com/abcdobjects/Core/Common/V1">
                        <listName xsi:nil="true"/>
                        <selection>
                           <id>2580244</id>
                           <apiName>BROADBAND_MEDIA</apiName>
                           <value>Broadband Media</value>
                        </selection>
                     </productLineS>
                  </row>
               </results>
            </responses>
         </response>
      </ns0:executeSavedQueryResponse>
   </S:Body>
</S:Envelope>

目前我在使用XPATH

时获得此输出
//row/productLineS/selection/value/text()

我明白了

          value
         Broadband Access
         Broadband Media
         Broadband Access

实际上,现在显示的值是3条记录。但实际上它只有两个记录

我的期望是这样的

           value
           Broadband Access,Broadband Media
           Broadband Access

因为只有2条记录

如何为这种情况定义XPATH

2 个答案:

答案 0 :(得分:0)

XPath(至少版本1)无法做到这一点。但是你使用XPath的语言可能就可以了。我猜你使用的是一种语言,因为没有#34; array&#34;在XML中。

例如,在xsh中,您可以编写

for //row echo @id xsh:join(",", productLineS/selection/value/text()) ;

并打印

1 Broadband Access,Broadband Media
2 Broadband Access

(在输入中将<row/>替换为</row>两次后。)

答案 1 :(得分:0)

XPath 2.0解决方案

首先,通过正确关闭未关闭的row元素,将元素包装在单个根元素中,并定义xsi命名空间前缀,使XML格式正确:

<r xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">            
  <row id="1">
    <productLineS attributeId="1004" >
      <listName xsi:nil="true"/>
      <selection>
        <id>2580243</id>
        <apiName>BROADBAND_ACCESS</apiName>
        <value>Broadband Access</value>
      </selection>
      <selection>
        <id>2580244</id>
        <apiName>BROADBAND_MEDIA</apiName>
        <value>Broadband Media</value>
      </selection>
    </productLineS>
  </row>  
  <row id="2">
    <productLineS attributeId="1004" >
      <listName xsi:nil="true"/>
      <selection>
        <id>2580243</id>
        <apiName>BROADBAND_ACCESS</apiName>
        <value>Broadband Access</value>
      </selection>
    </productLineS>
  </row>   
</r>

然后是这个XPath 2.0表达式,

for $r in //row return concat($r/@id, ' ', string-join($r//value, ','))

返回

1   Broadband Access,Broadband Media
2   Broadband Access

按要求。