简体   繁体   中英

How can I select those element which have no text children

In document below I am trying to get those element have no text child. but every effort get nothing.

<a>
      <b>
         <c> this is nice place </c>
      </b>

      <d> 
         <e> where this place is </e>
         <f> this place is very close to us</f>
      </d>

      <g> 
        <h/>
      </g>

     <i/>
</a>

How about this:

//*[not(node())]

Test query here

Stylesheet that copies empty elements (immediate children of a ) and their children, if there's not a single text node:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:apply-templates select="a"/>
  </xsl:template>

  <xsl:template match="a">
    <empty>
      <xsl:copy-of select="*[not(node()/text())]"/>
    </empty>
  </xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="utf-8"?>
<empty>
  <g>
    <h />
  </g>
  <i />
</empty>

所以,如果我理解正确的话,你希望所有的孩子a不具有文本节点?

/a/element()[not(.//text())]

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