Search code examples
xpathxslt-1.0exslt

XPATH needed for specific node(s) by attribute match


GIVEN XML

<lists>
<list>
<param type="a">content</param>
</list>
<list>
<param type="b">content2</param>
</list>
<list>
<param type="a">content</param>
<param type="b">content2</param>
</list>
<list>
<param type="c">content3</param>
</list>
</lists>

  1. I need to process any list/param[@type = 'a'|'b'] and I can do that, but I want the whole parent:list and loop when there may be more than one child::param and I can do that. I'm just not sure how to get the entire parent node with any param attribute based on the match of type 'a' or type 'b'. (XPATH needed)
  2. then I need to process any list without a param[@type = 'a'|'b'], and I can do that. But need the correct XPATH to get that as I'm going through the lists

My template right now just matches "list", but need a "branch"? choose/when/otherwise (?) - I'm not sure I have the XPATH syntax right. Thanks, I'm just trying to save time without trial and error!

I tried a lot of things, and know I have the incorrect paths. So just seeing if I have anything off.


Solution

  • The XPath for a list with a or b

    list[param[@type[. = 'a' or . = 'b']]]
    

    The XPath for a list without a or b

    list[not(param[@type[. = 'a' or . = 'b']])]
    

    OR

    The XPath for a list with a or b

    list[param[contains('ab', @type]]
    

    The XPath for a list without a or b

    list[not(param[contains('ab', @type])]