2013-10-17 86 views
0

我有一個pmd.xml文件看起來像這樣:找到所有節點與屬性值匹配另一

<file name="file1"> 
    <violation rule="rulename1" priority="1"> 
    </violation> 
    <violation rule="rulename2" priority="2"> 
    </violation> 
</file> 
<file name="file2"> 
    <violation rule="rulename2" priority="2"> 
    </violation> 
    <violation rule="rulename3" priority="1"> 
    </violation> 
</file> 

我使用XSLT文件中的另一個XML文件轉換這一點。

我現在試圖做的是: 對於每個衝突優先級= 1計算在整個XML中具有相同名稱的違規數量。稍後與Prio = 2相同。

,這樣我可以與他們經常出現在文檔中的計數,大致是這樣的列出所有的違規在一起:

prio1: 
rulename1, 1 
rulename3, 1 
prio2: 
rulename2, 2 

我不能找到一個妥善的XPath表達式來算違規問題同名...

<xsl:for-each select="//violation[@priority = 1]"> 
<xsl:value-of select="count(???)"/> 

任何人都有幫助的想法?

提前致謝!

+0

你運行XSLT 1.0或更高版本? – slashburn

+0

我改變了定義爲2.0,他沒有抱怨,所以我認爲我使用2.0 :) –

+0

我只是想知道是否可以使用for-each-group。否則,我們會像馬丁的解決方案一樣使用Muenchian分組。 您只更改了定義,還是安裝了XSLT 2.0?通常(使用JDK)你只有1.0。 – slashburn

回答

1

假設XSLT 1.0,您可以用鍵羣:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="text"/> 

<xsl:key name="k1" match="violation" use="@priority"/> 
<xsl:key name="k2" match="violation" use="concat(@priority, '|', @rule)"/> 

<xsl:template match="/"> 
    <xsl:apply-templates select="//violation[generate-id() = generate-id(key('k1', @priority)[1])]"/> 
</xsl:template> 

<xsl:template match="violation"> 
    <xsl:value-of select="concat('prio', @priority, ':&#10;')"/> 
    <xsl:apply-templates select="key('k1', @priority)[generate-id() = generate-id(key('k2', concat(@priority, '|', @rule))[1])]" mode="rule"/> 
</xsl:template> 

<xsl:template match="violation" mode="rule"> 
    <xsl:value-of select="concat(@rule, ', ', count(key('k2', concat(@priority, '|', @rule))), '&#10;')"/> 
</xsl:template> 

</xsl:stylesheet> 

變換

<files> 
<file name="file1"> 
    <violation rule="rulename1" priority="1"> 
    </violation> 
    <violation rule="rulename2" priority="2"> 
    </violation> 
</file> 
<file name="file2"> 
    <violation rule="rulename2" priority="2"> 
    </violation> 
    <violation rule="rulename3" priority="1"> 
    </violation> 
</file> 
</files> 

prio1: 
rulename1, 1 
rulename3, 1 
prio2: 
rulename2, 2 
+0

完美無缺,謝謝! :) –

相關問題