Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
775 views
in Technique[技术] by (71.8m points)

soap - Querying XML data with a long, multi special character, path name

Just getting started with XQuery using BaseX.

The XML structure that I did not create and have no control over, looks like this:

2002test.xml:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:runSearchResponse xmlns:ns2="http://externalapi.business.footprints.numarasoftware.com/">
      <return>
        <_items>
          <_containerDefinitionId>1234</_containerDefinitionId>
          <_containerDefinitionName>Service Desk</_containerDefinitionName>
          <_itemDefinitionId>2244</_itemDefinitionId>
          <_itemDefinitionName>Service Request</_itemDefinitionName>
          <_itemId>9989</_itemId>
          <_itemFields>
            <itemFields>
              <fieldName>Icon Name</fieldName>
              <fieldValue>
                <value>default_ticket.png</value>
              </fieldValue>
            </itemFields>
            <itemFields>
              <fieldName>emailCustomerUpdate</fieldName>
              <fieldValue>
                <value>false</value>
              </fieldValue>
            </itemFields>
            <itemFields>
              <fieldName>bestContactNumber_Customer</fieldName>
              <fieldValue>
                <value/>
              </fieldValue>
            </itemFields>
          </_itemFields>
        </_items>
      </return>
    </ns2:runSearchResponse>
  </soap:Body>
</soap:Envelope>

Doing a simple:

for $x in doc("2002test.xml")
return $x

returns my entire document as expected.

Any attempt to drill into it is not working for me however.

I've tried:

for $x in doc("2002test.xml")/soap:Envelope/soap:Body/ns2:runSearchResponse/return
return $x/_items/itemFields/fieldName

But the long path throws a "No namespace declared for 'soap:Envelope'."

So I tried declaring the path like this:

declare variable $m := "/soap:Envelope/soap:Body/ns2:runSearchResponse/return";
for $x in doc("2002test.xml")//$m
return $x

Which got me:

/soap:Envelope/soap:Body/ns2:runSearchResponse/return
/soap:Envelope/soap:Body/ns2:runSearchResponse/return
/soap:Envelope/soap:Body/ns2:runSearchResponse/return
...

I tried:

for $x in doc("2002test.xml")/*[local-name()='soap:Envelope'][local-name()='soap:Body'][local-name()='ns2:runSearchResponse'][local-name()='return']
    
return $x

Which did nothing.

Very simply I'd like to be able to do something like this:

for $x in doc("2002test.xml")
where $x/return/_items/_itemFields/itemFields/fieldValue/value="default_ticket.png"
return $x/return/_items/_itemFields/itemFields/fieldName/text()

resulting in:

Icon Name

Any advice?

question from:https://stackoverflow.com/questions/65602422/querying-xml-data-with-a-long-multi-special-character-path-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use basic XPath expressions with a predicate e.g.

doc("2002test.xml")//itemFields[fieldValue/value = "default_ticket.png"]/fieldName/text()

If you think you need a FLOWR expression then use e.g.

for $field in doc("2002test.xml")//itemFields
where $field/fieldValue/value = "default_ticket.png"
return $field/fieldName/text()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...