0

I have an xml file, I use the xpath /content which returns the following line:

<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

How can I get the type 'nil'? I am trying to write a test which will check if the content is empty, if there is no conten it will say nil="true" otherwise it will give the type e.g. xsi:type="String">true

I've tried //content[@xsi] and //content/xsi but still can't limit it down to just the part I want.

I could just get a substring but I think there must be a way to do it with xpath.

Doctor Who
  • 1,287
  • 5
  • 25
  • 46

1 Answers1

0

In your code snippet:

<content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>

there is a namespace. Although it does not apply to the element content, it does apply to the attribute nil. It is an attribute from the XML Schema specification, by the way.

To find an attribute that is in a namespace you either

  • cleanly register this namespace in your application and prefix the attribute in any XPath expression
  • ignore all namespaces in path expressions

You did not show any code, so I find it hard to comment on the first option. The second option means a path expression like

//content[@*[local-name() = 'nil'] = 'true']

will select content elements where the xsi:nil attribute value is "true".

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75