XPath - select 个节点只包含空格
XPath - select nodes only contains whitespaces
无论如何我可以 select 使用 xpath 的节点只包含空格 (  ,   , 	
)..
这里有一个例子,
<doc>
<p> </p>
<p> </p>
<p> </p>
<p>text</p>
<p> text</p>
<p> text</p>
</doc>
我需要 select 前 3 个 <p>
仅包含空白元素的节点
您只能将以下 xpath 用于 select 个包含空格的节点:
//*[normalize-space(text())='']
您需要应用 translate
并检查仅包含空格的此类节点的长度。
为您演示:http://xsltransform.net/ejivdHb/22
那么,尝试关注
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://locomotive/bypass/docx" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<!-- Select only node with white spaces -->
<xsl:if test="string-length(translate(., ' 	

','')) = 0">
<xsl:copy-of select="." />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
对于select p
不为空但只包含空白字符的节点,使用:
/doc/p[string() and not(normalize-space())]
例如,以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/doc">
<xsl:copy>
<xsl:copy-of select="p[string() and not(normalize-space())]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
应用于以下示例输入时:
XML
<doc>
<p/>
<p> </p>
<p> </p>
<p> </p>
<p>text</p>
<p> text</p>
<p> text</p>
</doc>
将return:
结果
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<p> </p>
<p> </p>
<p> </p>
</doc>
请注意,白色 space 的正常 XML 定义不包括 NBSP 字符 (xA0)。
到select节点包含一个或多个whitespace字符,没有别的,其中whitespace表示x9、xa、xd、x20和xa0,你可以做(在 XPath 2.0 中)
select="//*[matches(., '[	

  ]+')]"
或者您可以考虑
select="//*[matches(., '[\p{Z}]+')]"
匹配许多其他类似 space 的字符,例如 em-space、en-space、thin-space、hair-space , 表意 space, 等等
无论如何我可以 select 使用 xpath 的节点只包含空格 (  ,   , 	
)..
这里有一个例子,
<doc>
<p> </p>
<p> </p>
<p> </p>
<p>text</p>
<p> text</p>
<p> text</p>
</doc>
我需要 select 前 3 个 <p>
仅包含空白元素的节点
您只能将以下 xpath 用于 select 个包含空格的节点:
//*[normalize-space(text())='']
您需要应用 translate
并检查仅包含空格的此类节点的长度。
为您演示:http://xsltransform.net/ejivdHb/22
那么,尝试关注
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://locomotive/bypass/docx" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<!-- Select only node with white spaces -->
<xsl:if test="string-length(translate(., ' 	

','')) = 0">
<xsl:copy-of select="." />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
对于select p
不为空但只包含空白字符的节点,使用:
/doc/p[string() and not(normalize-space())]
例如,以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/doc">
<xsl:copy>
<xsl:copy-of select="p[string() and not(normalize-space())]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
应用于以下示例输入时:
XML
<doc>
<p/>
<p> </p>
<p> </p>
<p> </p>
<p>text</p>
<p> text</p>
<p> text</p>
</doc>
将return:
结果
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<p> </p>
<p> </p>
<p> </p>
</doc>
请注意,白色 space 的正常 XML 定义不包括 NBSP 字符 (xA0)。
到select节点包含一个或多个whitespace字符,没有别的,其中whitespace表示x9、xa、xd、x20和xa0,你可以做(在 XPath 2.0 中)
select="//*[matches(., '[	

  ]+')]"
或者您可以考虑
select="//*[matches(., '[\p{Z}]+')]"
匹配许多其他类似 space 的字符,例如 em-space、en-space、thin-space、hair-space , 表意 space, 等等