【发布时间】:2013-11-27 13:51:33
【问题描述】:
我在使用 XSLT 检查节点是否存在时遇到了一些问题,如果不存在则将它们添加到文档中。这是我的情况:
输入
<Message>
<a>123</a>
<c>456</c>
<d>789</d>
</Message>
期望的输出
<MsgHead>
<Document>
<Message>
<a>123</a>
<b>-1</b>
<c>456</c>
<d>789</d>
</Message>
<Document>
</MsgHead>
我还获得了以下带有“默认值”的静态文件
默认值
<DefaultNodes>
<a>-1</a>
<b>-1</b>
<c>-1</c>
<d>-1</d>
</DefaultNodes>
输入文件有不同数量的节点,我需要用缺少的默认节点“完成”它们。节点名称显然不是a、b、c等,而是大约700个不同默认值的不同节点。
这是我迄今为止的尝试 我的 XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<MsgHead>
<Document>
<Message>
<xsl:apply-templates></xsl:apply-templates>
</Message>
</Document>
</MsgHead>
</xsl:template>
<xsl:template match="Message">
<xsl:copy-of select="node()"/>
<xsl:for-each select="document('default-nodes.xml')/DefaultNodes/*">
<xsl:choose>
<xsl:when test="//*[local-name(current())]"> <!-- This is the line giving me trouble -->
<!--Node already present, do nothing-->
</xsl:when>
<xsl:otherwise>
<!--Node not in input, add from the defaults file -->
<xsl:copy-of select="self::node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这几乎可以工作,但似乎无法找到节点是否存在。我正在使用的当前测试 (//*[local-name(current())]) 似乎无论如何都会返回 true。有人对我如何解决这个问题有任何建议吗?
谢谢!
【问题讨论】: