Select all text not in div elements using xpath?
how can i select only text which is not in div tags?
eg.
<div>
<div>not this</div>
1<br/>
<div>not this</div>
1<br/>
1<br/>
<div>not this</div>
</div>
<div>
<div>not this</div>
2
<div>not this</div>
2
2
<div>not this</开发者_如何转开发div>
</div>
<div>
<div>not this</div>
3
<div>not this</div>
3
3
<div>not this</div>
</div>
results: {'1/n1/n1/n','2 2 2','3 3 3'}
//text()[normalize-space()][../node()[not(self::text())]]
Meaning: any not whitespace only text node having at least one sibling node
Use:
div/text()[string-length(normalize-space()) > 0]
This expression, when evaluated with the parent of the provided XML fragment as the context node, selects all non-white-space-only text-node children of any div
child of the context node.
Here is complete verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:for-each select=
"div/text()[string-length(normalize-space()) > 0]">
"<xsl:value-of select="."/>"
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML fragment (wrapped in a top element to become well-formed XML document):
<t>
<div>
<div>not this</div>
1<br/>
<div>not this</div>
1<br/>
1<br/>
<div>not this</div></div>
<div>
<div>not this</div>
2
<div>not this</div>
2
2
<div>not this</div></div>
<div>
<div>not this</div>
3
<div>not this</div>
3
3
<div>not this</div></div>
</t>
the wanted, correct result is produced:
"
1"
"
1"
"
1"
"
2
"
"
2
2
"
"
3
"
"
3
3
"
精彩评论