-1
 <categoryPath>
            <category>
                <id>01</id>
                <name>one</name>
            </category>
            <category>
                <id>02</id>
                <name>two</name>
            </category>
            <category>
                <id>03</id>
                <name>three</name>
            </category>
            <category>
                <id>04</id>
                <name>four</name>
            </category>
  </categoryPath>

I have a XML structure as seen above. Can anyone help me how to write XPath which can produce the below result? Please note that the number of category nodes will be dynamic and the values can be anything. The XPath should consider any number of available category elements(not fixed).

one -> two -> three -> four

Nirmal
  • 89
  • 6

2 Answers2

1

Assuming that the names of the tags <category> and <name> don't change, then this expression

categoryPath//category//name/text()

should output

one
two
three
four
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • This does not work for me. It returns the last note value(four) alone. – Nirmal Jul 10 '20 at 18:58
  • @Nirmal Doesn't make sense: [try it here](http://xpather.com/BsIevTE6). – Jack Fleeting Jul 10 '20 at 19:08
  • Thanks a lot for your response. I use Kony Mobile Fabric where I have to write this XPath in integration service to construct the result as seen in my question. I tried your XPath. However, I get only the last node value in the final response. Not sure whether it treats it differently as the values are separated in different lines. I would appreciate your help, if you can share another approach to get the result as "one -> two -> three -> four" in a same line. – Nirmal Jul 10 '20 at 19:21
  • @Nirmal I'm afraid I know nothing about Kony MobileFabric; your question was tagged "xpath", and this is definitely the correct xpath expression to get your targets. It's possible that MF's xpath support or processing is somehow different from standard xpath, but you'll have to ask them about that. – Jack Fleeting Jul 10 '20 at 19:29
  • Hi Jack, No worries. I just wanted to inform you in which application I use this XPath. Your XPath is valid and works in http://xpather.com/. But it does not work in Kony Mobile Fabric. I googled and found the below link. It looks like Mobile(Quantum) fabric does not support all the functions/ features of XPath 1.0. Not sure about this. I need to check with Kony team. Anyhow, thanks a lot for your help. https://docs.kony.com/konylibrary/konyfabric/kony_fabric_user_guide/Content/XPath.htm – Nirmal Jul 12 '20 at 08:04
  • @Nirmal Interesting; it's a pity kony doesn't have a playground like xpather to try things out on. I looked at your link, though, and the only thing I can think of is to try the expression like this: `categoryPath//category//name` and see if it works this way. – Jack Fleeting Jul 12 '20 at 10:21
1

With XPath 2.0, string-join(//category/name,",") will output : one,two,three,four but I'm not sure your software supports this.

With XPath 1.0, since you don't know the number of category elements, you can write an expression which supports up to 10 elements (add more if you want) :

substring-before(concat(//category[1]//name,",",//category[2]//name,",",//category[3]//name,",",//category[4]//name,",",//category[5]//name,",",//category[6]//name,",",//category[7]//name,",",//category[8]//name,",",//category[9]//name,",",//category[10]//name),",,")

Output : one,two,three,four

E.Wiest
  • 5,425
  • 2
  • 7
  • 12
  • I use these XPaths in Kony Mobile Fabric application. None of these work in Kony Mobile Fabric. Your XPaths are valid and I verified them in xpather.com. But they do not work in Kony Mobile Fabric. I googled and found the below link. It looks like Mobile(Quantum) fabric does not support all the functions/ features of XPath 1.0. Not sure about this. I need to check with Kony team. Anyhow, thanks a lot for your help. https://docs.kony.com/konylibrary/konyfabric/kony_fabric_user_guide/Content/XPath.htm – Nirmal Jul 12 '20 at 08:12