0

I am a newbie to XPath and am stuck in a small issue. Following is the sample XML I have.

<test>
    <OI>1626353388</OI>
    <PL>DL</PL>
    <OF>p</OF>
    <ele>
        <elet>
            <tpl>BR</tpl>
            <format>1</format>
        </elet>
    </ele>
</test>

I am trying to get a path by appending few strings:

concat('/app/test/1.0/forms/',/test/ele/elet/tpl,'.xd')

If format element is available in XML I need to append it with xd else it should be appended with pd. I am not sure how to do that. Can anybody help me with this? Thanks in advance.

meen
  • 2,287
  • 3
  • 23
  • 42
  • Which version of XPath are you using? There's an `if` statement in XPath 2.0, and a kludge exists in 1.0. See [this question](http://stackoverflow.com/questions/971067/is-there-an-if-then-else-statement-in-xpath) for more information. For the condition itself, `boolean(//format)` will return true if such an element exists. – jsheeran Jan 24 '17 at 08:29
  • If you are stuck with XPath 1.0, consider appending the file extension (or doing the entire string building) in the host language. It's going to be easier than trying to shoehorn it with XPath. – Tomalak Jan 24 '17 at 10:02
  • @Tomalak, thanks for the suggestion. I had thought this before. But not possible in my case as the XML I am getting is from third party. – meen Jan 24 '17 at 10:48
  • What does the origin of the XML have to do with anything? What language or tool are you using to process the XML? – Tomalak Jan 24 '17 at 10:50

1 Answers1

0

Found the logic to answer here: http://dreamix.eu/blog/java/xml-java/how-to-create-if-else-statement-in-xpath-1-0

Solution :

concat('/app/test/1.0/forms/', /test/ele/elet/tpl, '.', substring('pd', 1, number(/test/ele/elet/format) * string-length('pd')), substring('xd', 1, number(not(/test/ele/elet/format)) * string-length('xd')))
meen
  • 2,287
  • 3
  • 23
  • 42