我試圖將xml轉換爲新結構,然後在轉換中處理新結構。我的問題是讓新結構可用作節點集。我在webapp的lib中使用了Tomcat 6和saxon9he。爲了測試新結構的可用性,我創建了以下xsl。xslt 2.0可變元素節點集處理
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8"/>
<xsl:include href="nodeTest.xsl"/>
<xsl:variable name="theDoc">
<doc>
<source>from</source>
<source>here</source>
<target>to</target>
<target>there</target>
</doc>
</xsl:variable>
<xsl:template match="/">
<xsl:variable name="result1">
<xsl:call-template name="createLinks">
<xsl:with-param name="doc" select="$theDoc/doc/*"/>
</xsl:call-template>
</xsl:variable>
input:
<xsl:value-of select="$theDoc/doc/*"/>
output:
<xsl:value-of select="$result1"/>
</xsl:template>
</xsl:stylesheet>
我期望變量theDoc將包含一個有效的節點集,那createLinks模板將處理它作爲一個節點集。這裏是creatlelInks模板;
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="node()" mode="makeLink">
<xsl:param name="theSource" select="'source'"/>
<xsl:param name="theTarget" select="'target'"/>
<xsl:param name="theUsage" select="'usage'"/>
<xsl:element name="link" >
<xsl:element name="usage" ><xsl:value-of select="$theUsage"/></xsl:element>
<xsl:element name="source" ><xsl:value-of select="$theSource"/></xsl:element>
<xsl:element name="target" ><xsl:value-of select="$theTarget"/></xsl:element>
</xsl:element>
</xsl:template>
<xsl:template name="createLinks">
<xsl:param name="doc" select="*"/>
the input doc:
<xsl:value-of select="$doc"/>
<xsl:copy>
<xsl:element name="Links">
<xsl:apply-templates mode="makeLink" select=".">
<xsl:with-param name="theUsage" select="'link from source to target'"/>
<xsl:with-param name="theSource" select="$doc/source/*"/>
<xsl:with-param name="theTarget" select="$doc/target/*"/>
</xsl:apply-templates>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
執行時,結果如下所示;
<?xml version="1.0" encoding="UTF-8"?>
input:
from here to there
output:
the input doc:
fromheretotherelink from source to target
很明顯,Doc不是節點集。我的理解是,xslt 2.0標準將DOC作爲節點集,而不是結果樹片段。我如何更改這些簡單的xsls來將Doc的內容作爲節點集進行處理?
copy-of did顯示我期望的結果 - 在修改選擇語法之後,如您所指出的那樣...謝謝 – user2037942