值如何绑定到另一个绑定节点以获取该节点内的TEXT

时间:2014-06-18 03:43:21

标签: xml xslt

1我有一个XML和XSLT文件,我希望XSLT自动从同一XML文件中的另一个节点获取值。

XSLT第68行 我知道不应该这样,但我真的不知道如何将值传递给密钥(' Description2-by-id', 感谢

XML数据

<DATA>
    <CountryList>
        <CountryName code="AA" name="Antarctica" IsT="True"/>
        <CountryName code="AB" name="Abkhazia" IsT="False"/>
    </CountryList>
    <Description1List>
        <Description1Name Description1Id="1" RecordType="Person">P1</Description1Name>
        <Description1Name Description1Id="2" RecordType="Person">P2</Description1Name>
        <Description1Name Description1Id="1" RecordType="Entity">E1</Description1Name>
    </Description1List>
    <Description2List>
        <Description2Name Description2Id="1" Description1Id="2">P21</Description2Name>
        <Description2Name Description2Id="2" Description1Id="2">P22</Description2Name>
        <Description2Name Description2Id="3" Description1Id="3">E11</Description2Name>
    </Description2List>
    <Description3List>
        <Description3Name Description3Id="1" Description2Id="1">P211</Description3Name>
        <Description3Name Description3Id="2" Description2Id="3">E111</Description3Name>
    </Description3List>
    <ReferencesList>
        <ReferenceName code="9811" name="National List" Description2Id="1"/>
    </ReferencesList>
    <Records>
        <Person id="752" date="15-Oct-2013">
            <Country CountryType="BB">
                <CountryValue>AA</CountryValue>
            </Country>
            <Descriptions>
                <Description Description1="1" Description2="2" Description3="1"/>
                <Description Description1="2"/>
            </Descriptions>
            <References>
                <Reference>9811</Reference>
            </References>
        </Person>
        <Entity id="758" date="15-Oct-2013">
            <Country CountryType="BC">
                <CountryValue>AB</CountryValue>
            </Country>
            <Descriptions>
                <Description Description1="1" Description2="3" Description3="2"/>
                <Description Description1="3"/>
            </Descriptions>
        </Entity>
    </Records>
</DATA>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="country-by-code" match="CountryName" use="@code"/>
    <xsl:key name="Description1" match="Description1Name" use="concat(@Description1Id, '|', @RecordType)"/>
    <xsl:key name="Description2-by-id" match="Description2Name" use="@Description2Id"/>
    <xsl:key name="Description3-by-id" match="Description3Name" use="@Description3Id"/>
    <xsl:key name="Reference-by-code" match="ReferenceName" use="@code"/>
    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <tr>
                        <th>ID</th>
                        <th>Date</th>
                        <th>Country</th>
                        <th>Description1</th>
                        <th>Description2</th>
                        <th>Description3</th>
                        <th>Reference</th>
                        <th>ReferenceName</th>
                        <th>ReferenceDescription2code</th>
                        <th>ReferenceDescription2</th>
                    </tr>
                    <xsl:apply-templates select="DATA/Records/*"/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Person | Entity">
        <xsl:variable name="varRrecord" select="."/>
        <xsl:for-each select="$varRrecord/Country">
            <xsl:variable name="varCountry" select="self::Country"/>
            <xsl:for-each select="$varRrecord/Descriptions/Description | $varRrecord[not(Descriptions/Description)]">
                <xsl:variable name="varDescription" select="self::Description"/>
                <xsl:for-each select="$varRrecord/References/Reference | $varRrecord[not(References/Reference)]">
                    <xsl:variable name="varReference" select="self::Reference"/>
                    <tr>
                        <td>
                            <xsl:value-of select="$varRrecord/@id"/>
                        </td>
                        <td>
                            <xsl:value-of select="$varRrecord/@date"/>
                        </td>
                        <td>
                    [<xsl:value-of select="$varCountry/@CountryType"/>]
                    <xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@name"/>
                    [<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@IsT"/>]
                </td>
                        <td>
                            <xsl:value-of select="key('Description1', concat($varDescription/@Description1, '|', name($varRrecord)))"/>
                        </td>
                        <td>
                            <xsl:value-of select="key('Description2-by-id', $varDescription/@Description2)"/>
                        </td>
                        <td>
                            <xsl:value-of select="key('Description3-by-id', $varDescription/@Description3)"/>
                        </td>
                        <td>
                            <xsl:value-of select="$varReference"/>
                        </td>
                        <td>
                            <xsl:value-of select="key('Reference-by-code', $varReference)/@name"/>
                        </td>
                        <td>
                            <xsl:value-of select="key('Reference-by-code', $varReference)/@Description2Id"/>
                        </td>
                        <td>
                            <xsl:value-of select="key('Description2-by-id', <xsl:value-of select="key('Reference-by-code', $varReference)/@Description2Id"/>)"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

实际结果是

|ID  |Date        |Country                |Description1  |Description2  |Description3  |Reference  |ReferenceName  |ReferenceDescription2code  |ReferenceDescription2| 
|752 |15-Oct-2013 |[BB] Antarctica [True] |P1            |P22           |P211          |9811       |National List  |1                          |                     |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P2            |              |              |9811       |National List  |1                          |                     |
|758 |15-Oct-2013 |[BC] Abkhazia [False]  |E1            |E11           |E111          |           |               |                           |                     |
|758 |15-Oct-2013 |[BC] Abkhazia [False]  |              |              |              |           |               |                           |                     |

我期望的结果是什么

|ID  |Date        |Country                |Description1  |Description2  |Description3  |Reference  |ReferenceName  |ReferenceDescription2code  |ReferenceDescription2| 
|752 |15-Oct-2013 |[BB] Antarctica [True] |P1            |P22           |P211          |9811       |National List  |1                          |P21                  |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P2            |              |              |9811       |National List  |1                          |P21                  |
|758 |15-Oct-2013 |[BC] Abkhazia [False]  |E1            |E11           |E111          |           |               |                           |                     |
|758 |15-Oct-2013 |[BC] Abkhazia [False]  |              |              |              |           |               |                           |                     |

1 个答案:

答案 0 :(得分:1)

再次?

我担心这个例子不够清楚(太多相似的值)并且没有逻辑解释(!),所以这只是一个猜测:

<td>
    <xsl:value-of select="key('Description2-by-id', key('Reference-by-code', $varReference)/@Description2Id)"/>
</td>

这样做是(1)从匹配的@Description2Id获取ReferenceName值,(2)使用它通过Description2Name键获取'Description2-by-id'