質問

I need an XPath expression that verifies that an XML list element has only a certain type of node. example:

I need to know if the list has only images.

<?xml version="1.0" encoding="UTF-8"?>

<mc type="group"> 
  <mc type="list"> 
    <mc type="group"> 
      <mc type="image"/> 
    </mc>  
    <mc type="group"> 
      <mc type="image"/> 
    </mc>  
    <mc type="group"> 
      <mc type="image"/> 
    </mc>  
    <mc type="group"> 
      <mc type="image"/> 
    </mc> 
  </mc> 
</mc>

the above XML is TRUE

<?xml version="1.0" encoding="UTF-8"?>

<mc type="group"> 
  <mc type="list"> 
    <mc type="group"> 
      <mc type="image"/> 
    </mc>  
    <mc type="group"> 
      <mc type="image"/> 
      <mc type="text"/>
    </mc>  
    <mc type="group"> 
      <mc type="image"/> 
    </mc>  
    <mc type="group"> 
      <mc type="image"/> 
    </mc> 
  </mc> 
</mc>

the above XML is FALSE

役に立ちましたか?

解決

Use:

not(//mc[not(mc) and @type[not(. = 'image')]])

This evaluates to true() if and only if there isn't a "leaf" mc element the string-value of whose type attribute is different from the string "image".

Explanation: Proper use of the "double-negation law".

他のヒント

You can check not(/mc/mc[@type = 'list']//mc[not(@type = 'image')]) if you start with the document node as the context node or not(.//mc[not(@type = 'image')]) if you start with the mc element with the type="list" attribute.

You could use negation of one-or-more: not(one-or-more(mc[not(@type = 'image')])) or some such.

Or the stupid/simple method, check for all mc elements if they are not an image/group/list:

not(//mc[not(@type = ("image", "group", "list"))])
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top