2014-05-21 27 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