w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
XPath finding all nodes whit an attribute which have the same value as a certain node attribute

Assuming XSLT 1.0 you can group with keys:

<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>

Transforms

<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>

into

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




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.