0
我試圖在XSLT中使用查找表。我有以下個XML:XSLT查找表:從查找中用元素替換元素
data.xml中:
<?xml version="1.0"?>
<labels>
<label>
<name>Thomas Eliot</name>
<address>
<city>Hartford</city>
<state id= "CT"/>
</address>
</label>
<label>
<name>Ezra Pound</name>
<address>
<city>Hailey</city>
<state>
<name>New York</name>
</state>
</address>
</label>
</labels>
lookup.xml:
<?xml version="1.0"?>
<states>
<state id="CT">
<name>Connecticut</name>
</state>
</states>
對於輸出我想:
<labels>
<label>
<name>Thomas Eliot</name>
<address>
<city>Hartford</city>
<state>
<name>Connecticut</name>
</state>
</address>
</label>
<label>
<name>Ezra Pound</name>
<address>
<city>Hailey</city>
<state>
<name>New York</name>
</state>
</address>
</label>
</labels>
所以我想解決data.xml中的狀態ID並從lookup.xml中複製相應元素的內容
我堅持了以下的xsl:
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="lookupKey" match="state" use="@id"/>
<xsl:variable name="lookupStore" select="document('lookup.xml')/states/"/>
<xsl:template match="state[@id]">
<xsl:apply-templates select="$lookupStore">
<xsl:with-param name="current" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="states">
<xsl:param name="current"/>
<xsl:value-of select="key('lookupKey', $current/address/state/@id)/."/>
</xsl:template>
</xsl:transform>
爲什麼<xsl:template match="state[@id]">
不適?
謝謝!我是如何錯過它的.. – Bruckwald