每个具有键功能的XSLT

时间:2014-12-11 12:00:10

标签: xml xslt

我的XSLT文件有问题。

我有一个看起来像这样的XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Collection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="project5.xsd">
    <NormCollection>
        <Norm id="1">
            <Name>EN ISO 13857</Name>
            <Keyword>Security</Keyword>
            <Keyword>Engines</Keyword>
            <ReplacementFor>-</ReplacementFor>
        </Norm>
        <Norm id="2">
            <Name>EN ISO 13849-1</Name>
            <Keyword>Security</Keyword>
            <Keyword>Engines</Keyword>
            <Keyword>Controlling</Keyword>
            <ReplacementFor>1</ReplacementFor>
        </Norm>
    </NormCollection>
    <PublicationCollection>
        <Publication idpub="100">
            <Title>Norms for Engine Security</Title>
            <Filename>caise2011sgln.pdf</Filename>
            <RefNorm idref="1" />
            <RefNorm idref="2" />
        </Publication>
        <Publication idpub="200">
            <Title>Setting up Complex Engines</Title>
            <Filename>coopis2012sln.pdf</Filename>
            <RefNorm idref="2" />
        </Publication>
    </PublicationCollection>
</Collection>

一个看起来像这样的XSLT文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:key name="key" match="Collection/PublicationCollection/Publication/RefNorm" use="@idref"/> 
<xsl:template match="/">
<table>
<tr>
<td>Norm</td>
<td>Publications</td>
</tr>
<xsl:for-each select="key('key',Collection/NormCollection/Norm/@id)/.">
<tr>
<td><xsl:value-of select="../../../NormCollection/Norm/Name"/></td>
<td><xsl:value-of select="../Title"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

输出如下:

Norm    Publications
EN ISO 13857    Norms for Engine Security
EN ISO 13857    Norms for Engine Security
EN ISO 13857    Setting up Complex Engines

我的问题是它应该是这样的:

Norm    Publications
EN ISO 13857    Norms for Engine Security
EN ISO 13857    Norms for Engine Security
EN ISO 13849-1  Setting up Complex Engines

我现在已经尝试了一切,但我无法获得正确的结果。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为你的意思是:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="key" match="Publication" use="RefNorm/@idref"/> 

<xsl:template match="/">
    <table>
        <tr>
            <th>Norm</th>
            <th>Publications</th>
        </tr>
        <xsl:for-each select="Collection/NormCollection/Norm">
            <xsl:variable name="name" select="Name" />
            <xsl:for-each select="key('key', @id)">
                <tr>
                    <td><xsl:value-of select="$name"/></td>
                    <td><xsl:value-of select="Title"/></td>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

但是,结果与您向我们展示的结果不同:

Norm            Publications
EN ISO 13857    Norms for Engine Security
EN ISO 13849-1  Norms for Engine Security
EN ISO 13849-1  Setting up Complex Engines