我有以下 XML
<ns0:Root xmlns:ns0="http://Map_Sample.Input_Schema">
<Number1>
</Number1>
<Number2>11</Number2>
<Number3>12</Number3>
</ns0:Root>
在这种情况下我得到ENTER
or \r\n
for Number1
。我想编写一些 XSLT 来检查节点是否包含ENTER
or \r\n
然后更换它<Number1 />
.
谁能帮我为此编写 XSLT?
“空元素”的含义有不同的可能定义.
让我们定义一个空元素作为一个没有任何元素节点子节点且其字符串值为空字符串或仅由空白字符(' '、CR、NL)组成的节点。
然后要检查给定元素是否为空,请使用:
not(*) and not(normalize-space())
这假定在计算 XPath 表达式时该元素是上下文节点。
在本例中,我得到 Number1 的“ENTER”或“\r\n”。我想要
编写 XSLT 检查节点是否包含“ENTER”或“\r\n”然后替换它
。
您尚未指定应替换该文本节点的内容,因此在此解决方案中,我假设文本节点应替换为空字符串(已删除):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(*) and not(normalize-space())]">
<xsl:element name="{name()}" namespace="{namespace-uri()}"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<ns0:Root xmlns:ns0="http://Map_Sample.Input_Schema">
<Number1>
</Number1>
<Number2>11</Number2>
<Number3>12</Number3>
</ns0:Root>
产生了想要的结果:
<ns0:Root xmlns:ns0="http://Map_Sample.Input_Schema">
<Number1/>
<Number2>11</Number2>
<Number3>12</Number3>
</ns0:Root>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)