我有一个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 :(得分: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, ': ')"/>
<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))), ' ')"/>
</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