我在使用XSLT檢查節點是否存在以及是否將它們添加到文檔中時遇到了一些問題。這裏是我的情況: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。任何人有任何建議我如何解決這個問題?
謝謝!
作品像魅力,比我的方法更優雅!謝謝一堆! – Solvemon