Prakash
Prakash

Reputation: 659

Xpath to find the root of the XML using Spring DSL

I am using Spring DSL, to access webserver, like this,

<route>
    <!-- 1 -->
    <from uri="...">
    <!-- 2 -->
    <to uri="...">
    <!-- 3 -->
    <choice>
        <when>
            <xpath></xpath>
            <to uri="...">
        </when>
        <when>
            <xpath></xpath>
            <to uri="...">
        </when>
    </choice>
</route>

<!-- 1 --> when the Endpoint hits, <!-- 2 --> sending a request to the web server, <!-- 3 --> checking the root element which is received as a response from the web server, based on that response XML will send to another endpoint

Webserver will return a either one of the 2 XML message like,

<tns:roottag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xmlns:tns="http://example.com">
    <tns:leaftag>
        information
    </tns:leaftag>
</tns:roottag>

or

<tns:Parenttag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xmlns:tns="http://example.com">
    <tns:Childtag>
        information
    </tns:Childtag>
</tns:parenttag>

after receiving the XML from the web server, need to check the root based on that different operation will be performed on that received XML,

After referring to some website, I came to know that XPath in spring DSL can be used for the condition,

My question: 1. Only retrieve the root tag name from the response XML (as like below ) and check with the XPath based on that perform a different operation with the original response XML

tns:Parenttag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xmlns:tns="http://example.com" ==> Parenttag

or

tns:roottag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xmlns:tns="http://example.com" ==> roottag

Upvotes: 0

Views: 110

Answers (1)

Tomalak
Tomalak

Reputation: 338288

This will only match when the top-level element is a <tns:roottag>:

<xpath>/tns:roottag</xpath>

and this will only match when the top-level element is a <tns:Parenttag>:

<xpath>/tns:Parenttag</xpath>

However, before this can work, you need to declare the tns prefix. You can do that on the <beans> top element:

<beans
    xmlns="http://www.springframework.org/schema/beans"
    ...other namespace declarations...
    xmlns:tns="http://example.com"
>

Make sure the namespace URI matches the one in the XML responses.

Upvotes: 1

Related Questions