2015-04-30 200 views
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]">不適?

回答

1

找到差異:

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:variable name="lookupStore" select="document('lookup.xml')/states"/> 

<xsl:key name="lookupKey" match="state" use="@id"/> 

<xsl:template match="state[@id]"> 
    <xsl:copy> 
     <xsl:apply-templates select="$lookupStore"> 
      <xsl:with-param name="current" select="."/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="states"> 
    <xsl:param name="current"/> 
    <xsl:copy-of select="key('lookupKey', $current/@id)/name"/> 
</xsl:template> 

如果你願意,你可以縮短整個事情:

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:variable name="lookupdoc" select="document('lookup.xml')"/> 

<xsl:key name="state-by-id" match="state" use="@id"/> 

<xsl:template match="state/@id"> 
    <xsl:variable name="id" select="."/> 
    <!-- switch context to the lookup document --> 
    <xsl:for-each select="$lookupdoc"> 
     <xsl:copy-of select="key('state-by-id', $id)/name"/> 
    </xsl:for-each> 
</xsl:template> 
+0

謝謝!我是如何錯過它的.. – Bruckwald