简体   繁体   中英

How can I select parent elements that contain empty element with XPath?

I have the following xml:

<rootelement>
   <parentelement>
      <mytype>123</mytype>
      <myvalue>abc</myvalue>
   </parentelement>
   <parentelement>
      <mytype></mytype>
      <myvalue>xyz</myvalue>
   </parentelement>
   <parentelement>
      <mytype />
      <myvalue>qwe</myvalue>
   </parentelement>
   <parentelement>
      <myvalue>asdf</myvalue>
   </parentelement>
</rootelement>

I want to use XPath to select:

   <parentelement>
      <mytype></mytype>
      <myvalue>xyz</myvalue>
   </parentelement>
   <parentelement>
      <mytype />
      <myvalue>qwe</myvalue>
   </parentelement>
   <parentelement>
      <myvalue>asdf</myvalue>
   </parentelement>

When I try this in my xslt:

<xsl:template match="/rootelement/parentelement/mytype[not(text())]/.."/>

it fails with:

Unexpected token in pattern, found ".."

When I try this:

<xsl:template match="/rootelement/parentelement/mytype[not(text())]/parent::*"/>

it fails with:

Axis in pattern must be child or attribute

How can I select parent elements that contain empty mytype?

Thanks!

匹配元素并将条件放入谓词中,例如

<xsl:template match="/rootelement/parentelement[mytype[not(node())]]"/>

You say "parent elements that contain empty mytype". So you select parentelement , followed by a condition: it should contain mytype , and mytype should be empty. By "empty", I assume that you mean no text or elements, so node() is preferrable:

/rootelement/parentelement[mytype[not(node())]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM