1

I have a XML File Like this below, where i have a array under array. Here i have only 2 records

<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>

Currently i am getting this output when i use XPATH

//row/productLineS/selection/value/text()

I get this

          value
         Broadband Access
         Broadband Media
         Broadband Access

Actually, value showing now is 3 records. But in real it is only two records

My expectation is like this

           value
           Broadband Access,Broadband Media
           Broadband Access

As there are only 2 records

How to define XPATH for such situation

1
  • How can the XPath expression return id? Please show the full code. Commented Sep 5, 2017 at 17:10

2 Answers 2

1

XPath 2.0 Solution

First, make your XML be well-formed by properly closing the unclosed row elements, wrapping elements in a single root element, and defining the xsi namespace prefix:

<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>

Then this XPath 2.0 expression,

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

returns

1   Broadband Access,Broadband Media
2   Broadband Access

as requested.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks for your response. I used your code here codebeautify.org/Xpath-Tester but not working. May be please take look at above improvized xml file
@ritesh: Then that site doesn't support XPath 2.0, because this solution is correct and verified. Here's an online site that does support XPath 2.0.
Hi. Please take a look on my modfied xml, pasted above. Please see if you can provide XPATH for that. Thanks
i tried this its working in Xpath 2.0. return concat($r/@id, ' ', string-join($r//productLineS//value, ',')) .How can we do same in 1.0
If you can't use XPath 2.0, and you can't use the facilities of the language hosting XPath 1.0 (such as XSLT, or xsh as choroba shows), then you're out of luck. XPath, especially XPath 1.0, is for selection, and you're basically asking for transformation -- a fundamental mismatch.
0

XPath (at least version 1) can't do that. But the language you use XPath in probably can. I guess you use a language, because there's no notion of "array" in XML.

For example, in xsh, you can write

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

and it prints

1 Broadband Access,Broadband Media
2 Broadband Access

(after replacing <row/> by </row> twice in the input).

1 Comment

Hi. Thanks for your response. I am not using xsh. Please take a look at above xml file. I have flushed full xml. I thought no one will respond so i gave just example tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.