2014-05-21 26 views
0

我有以下xml文件。根据属性值从xsl:key返回的包中删除重复项

<Bank> 
    <Person personId="1" type="1071" deleted="0"> 
    </Person> 
    <Person personId="2" type="1071" deleted="0"> 
    </Person> 
    <Person personId="3" type="1071" deleted="0"> 
    </Person> 
    <Account> 
    <Role personId="1" type="1025" /> 
    </Account> 
    <Account> 
    <Role personId="1" type="1025" /> 
    </Account> 
    <Account> 
    <Role personId="1" type="1018" /> 
    </Account> 
    <Account> 
    <Role personId="3" type="1025" /> 
    <Role personId="1" type="1018" /> 
    </Account> 
    <Account> 
    <Role personId="2" type="1025" /> 
    </Account> 
</Bank> 

和下面的XSL转换。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" encoding="ISO-8859-1" /> 
    <xsl:strip-space elements="*" /> 

    <xsl:key name="roleKey" 
    match="Role[(@type = '1025' or @type = '1018' or @type = '1022' or @type = '1023') and not(@validTo)]" 
    use="@personId" /> 

    <xsl:template match="Person"> 
    <xsl:value-of select="@personId" /> 
    <xsl:variable name="roles" select="key('roleKey', @personId)" /> 
    <xsl:for-each select="$roles"> 
     <xsl:text>;</xsl:text><xsl:value-of select="@type" /> 
    </xsl:for-each> 
    <xsl:text>&#xA;</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

实际的结果如下,我想删除重复的type值。

1;1025;1025;1018;1018 
2;1025 
3;1025 

预期的结果应该是这样如下...

1;1025;1018 
2;1025 
3;1025 

我曾尝试涉及来自this website关键字following同时还与Muenchian方法的伎俩提示。他们都不工作,因为他们似乎在浏览整个文档并为每个Person元素匹配重复项,而我只想在personId属性定义的Person上下文中删除重复项。

如何从key函数返回的包中删除那些重复项?或者,也许有一种方法,我可以在xsl:for-each中使用,只打印我想要的内容?

我只能使用XSLT 1.0中的可用内容。我没有可能使用任何XSLT 2.0处理器。

回答

1

好吧,不知道这是否是最好的解决方案,但我通过引入这样的密钥并使用Muenchian方法来实现我想要的。

<xsl:key name="typeKey" match="Role" use="concat(@type, '|', @personId)" /> 

整个转型看起来像变更后...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" encoding="ISO-8859-1" /> 
    <xsl:strip-space elements="*" /> 

    <xsl:key name="roleKey" 
    match="Role[(@type = '1025' or @type = '1018' or @type = '1022' or @type = '1023') and not(@validTo)]" 
    use="@personId" /> 
    <xsl:key name="typeKey" match="Role" use="concat(@type, '|', @personId)" /> 

    <xsl:template match="Person"> 
    <xsl:value-of select="@personId" /> 
    <xsl:variable name="roles" select="key('roleKey', @personId)" /> 
    <xsl:for-each select="$roles[generate-id() = generate-id(key('typeKey', concat(@type, '|', @personId)))]"> 
     <xsl:text>;</xsl:text><xsl:value-of select="@type" /> 
    </xsl:for-each> 
    <xsl:text>&#xA;</xsl:text> 
    </xsl:template> 
</xsl:stylesheet> 

与实际结果是现在...

1;1025;1018 
2;1025 
3;1025