试着学习XSL,我无法将它直接记在我的脑海里。比方说,我想要创建一个XSLT模板,该模板根据节点的值处理节点。让我们来做一个示例情况:
<data>
<item1>0</item1>
<item2></item2>
<!--Multiple similar nodes below-->
</data>让我们尝试用xsi:nil="true"标记空的,而将零的标记为:
<xsl:template match="node()[starts-with(name(), 'item')]">
<xsl:copy>
<xsl:if test="not(node()) and not(.!='')"><!--This is the problem-->
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>这确实做了我想要做的事情,但这是弗兰肯斯坦的怪物代码,我还没有完全理解。
现在,在进行实验、研究堆栈溢出并试图找出它时,我发现not(node())可以同时定义空节点和零值节点,也可以定义.!=0,但是.!=''似乎只针对零值节点,这对我来说没有任何意义。
在我看来,字符串值可能被限定为零值,或者零值被限定为空字符串值,但我无法在此基础上计算出来。
基本上,问题是如何区分空节点和具有零值的节点。
发布于 2016-07-26 08:25:39
它确实将xsi:nil="true“添加到空节点,但由于任何原因,它删除了零的item1值,使其为空。
那是因为你已经把指令:
<xsl:apply-templates select="@* | node()"/>在xsl:if语句中。
基本上,问题是如何区分空节点和零值节点?
空节点没有子节点-元素、文本节点或任何其他节点类型(属性除外)。具有零值的节点具有字符串值为"0“的子文本节点。要测试空元素,只需执行以下操作即可:
<xsl:template match="*[starts-with(name(), 'item')][not(node())]">
<xsl:copy>
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:copy>
</xsl:template>https://stackoverflow.com/questions/38584350
复制相似问题