Search code examples
xpathinfopath

XPath: Count true child nodes with different names?


Is it possible to count the following TRUE nodes (with different names) with XPath in the XML below?

<my:templateBase>
  <my:executive>true</my:executive>
  <my:seniorManager>false</my:seniorManager>
  <my:manager>false</my:manager>
  <my:supervisor>false</my:supervisor>
  <my:directReport>false</my:directReport>
</my:templateBase>

I can't seem to work out the XPATH, e.g. count(templateBase/*[?=true()]).


Solution

  • Find all elements which contain the text "true" and count them.

    count(//my:templateBase/*[text() = 'true'])
    

    Make sure to correctly register the namespace or use * instead of my as wildcard namespace, for example

    count(//*:templateBase/*[text() = 'true'])
    

    I do not know Infopath, maybe it also just omits namespaces.

    If you also want to search subnodes for the text "true", use

    count(//my:templateBase//*[text() = 'true'])