0

My xml look a bit like this ...

<DataItems>
  <DataItem name="Order Number" type="string">ABC1234</DataItem>
  <DataItem name="Customer Id" type="integer">667744</DataItem>
  <DataItem name="Customer Name" type="string">Ronnie Pickering</DataItem>
</DataItems>

I'm trying to use XPath 1.0 to return a boolean based on whether the Order Number start with a certain string - "ABC"

I can't figure it out at all. Help, please.

Michael Vincent
  • 1,620
  • 1
  • 20
  • 46

1 Answers1

1

This XPath,

//DataItem[@name='Order Number' and starts-with(., 'ABC')]

will return all DataItem elements whose name is 'Order Number' and whose string value starts with 'ABC'.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • That's great, thanks. I've added a string-length() > 0 to it to give me a boolean. Regards, – Michael Vincent Nov 16 '17 at 16:39
  • @MichaelVincent: Not sure what you're shooting for with `string-length() > 0`, but if it's specifically a boolean you're looking for, you could wrap the above XPath in `boolean()`. – kjhughes Nov 16 '17 at 16:45
  • your answer returns a string - if I wrap that in boolean() what does it test for for true? A non-empty string? Or? Anyway, that's what I was shooting for. Regards, – Michael Vincent Nov 16 '17 at 17:13
  • No, my answer returns a nodeset in XPath 1.0 and a sequence in XPath 2.0, both of elements. Wrapping it in `boolean()` will return false if empty, true otherwise. If you're seeing the result as a string, then it is being passed through an external function, perhaps in the hosting language or calling system, that is turning it into a string. – kjhughes Nov 16 '17 at 17:51