[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Pattern Matching in XSl - find groups defined in one Xml in another Xml.


Subject: Re: [xsl] Pattern Matching in XSl - find groups defined in one Xml in another Xml.
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 21 Aug 2012 10:16:15 -0400

At 2012-08-21 13:38 +0000, Kerry, Richard wrote:
How can I use XSL to detect patterns in one XML file defined in another ?

For your first example, using keys.


I have two files :
1.  The data I'm given.
2.  The file I've created with the patterns I want to find.

An abridged example of the given data might be :
...

If the pattern data is:
...

The result would be :
<areas>
<area name="LAR 23">
<alarm equipment="1 Alarm" found="group-2" />
<alarm equipment="3 Alarm" found="group-2" />
<alarm equipment="MDU X2301 Relay fail"/>
<alarm equipment="2 Alarm" found="group-2" />
<alarm equipment="MDU X2301 Fuse fail"/>
</area>
...

You can see that a "found" attribute has been added wherever a pattern from the pattern file has been found. The pattern must be found in its entirety (ie LAR 51 doesn't find "group-2" as "2 Alarm" is not present). The order of items is unimportant (ie LAR 23 does find group-2 despite not being in the order it's defined in).

A running example is below.


- Supplementary question.
Can this be extended to find patterns defined using regular expressions ?

I'm still thinking on that, but I'm heading out for an obligation so I thought I'd post this before leaving.


I hope it helps.

. . . . . . . . . . . Ken

~/t/ftemp $ cat groups.xml
<groups>
<group id="group-1">
<alarm equipment="Frame 1 PSU 1"/>
<alarm equipment="Frame 1 PSU 2"/>
<alarm equipment="Fan Tray 1"/>
</group>
<group id="group-2">
<alarm equipment="1 Alarm"/>
<alarm equipment="2 Alarm"/>
<alarm equipment="3 Alarm"/>
</group>
</groups>~/t
~/t/ftemp $ cat robert.xml
<areas>
<area name="LAR 23">
<alarm equipment="1 Alarm"/>
<alarm equipment="3 Alarm"/>
<alarm equipment="MDU X2301 Relay fail"/>
<alarm equipment="2 Alarm"/>
<alarm equipment="MDU X2301 Fuse fail"/>
</area>
<area name="LAR 41">
<alarm equipment="MDU X4107 Relay fail"/>
<alarm equipment="MDU X4107 Fuse fail"/>
<alarm equipment="MDU X4108 Relay fail"/>
<alarm equipment="MDU X4108 Fuse fail"/>
<alarm equipment="Frame 1 PSU 1"/>
<alarm equipment="Frame 1 PSU 2"/>
<alarm equipment="Fan Tray 1"/>
</area>
<area name="LAR 51">
<alarm equipment="1 Alarm"/>
<alarm equipment="3 Alarm"/>
<alarm equipment="Frame 1 PSU 1"/>
<alarm equipment="Frame 1 PSU 2"/>
<alarm equipment="Fan Tray 1"/>
</area>
<area name="LAR 53">
<alarm equipment="1 Alarm"/>
<alarm equipment="2 Alarm"/>
<alarm equipment="Rack 5308 Power Fail Alarm"/>
<alarm equipment="Rack 5309 Power Fail Alarm"/>
<alarm equipment="3 Alarm"/>
</area>
<area name="LAR63">
<alarm equipment="MDU X6311 Relay fail"/>
<alarm equipment="MDU X6311 Fuse fail"/>
</area>
</areas>~/t/ftemp $
~/t/ftemp $ xslt2 robert.xml robert.xsl
<?xml version="1.0" encoding="UTF-8"?><areas>
<area name="LAR 23">
<alarm equipment="1 Alarm" found="group-2"/>
<alarm equipment="3 Alarm" found="group-2"/>
<alarm equipment="MDU X2301 Relay fail"/>
<alarm equipment="2 Alarm" found="group-2"/>
<alarm equipment="MDU X2301 Fuse fail"/>
</area>
<area name="LAR 41">
<alarm equipment="MDU X4107 Relay fail"/>
<alarm equipment="MDU X4107 Fuse fail"/>
<alarm equipment="MDU X4108 Relay fail"/>
<alarm equipment="MDU X4108 Fuse fail"/>
<alarm equipment="Frame 1 PSU 1" found="group-1"/>
<alarm equipment="Frame 1 PSU 2" found="group-1"/>
<alarm equipment="Fan Tray 1" found="group-1"/>
</area>
<area name="LAR 51">
<alarm equipment="1 Alarm" found="group-2"/>
<alarm equipment="3 Alarm" found="group-2"/>
<alarm equipment="Frame 1 PSU 1" found="group-1"/>
<alarm equipment="Frame 1 PSU 2" found="group-1"/>
<alarm equipment="Fan Tray 1" found="group-1"/>
</area>
<area name="LAR 53">
<alarm equipment="1 Alarm" found="group-2"/>
<alarm equipment="2 Alarm" found="group-2"/>
<alarm equipment="Rack 5308 Power Fail Alarm"/>
<alarm equipment="Rack 5309 Power Fail Alarm"/>
<alarm equipment="3 Alarm" found="group-2"/>
</area>
<area name="LAR63">
<alarm equipment="MDU X6311 Relay fail"/>
<alarm equipment="MDU X6311 Fuse fail"/>
</area>
</areas>~/t/ftemp $
~/t/ftemp $ cat robert.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:key name="grouped" match="group/alarm" use="@equipment"/>

<xsl:template match="alarm">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each select="key('grouped',@equipment,doc('groups.xml'))">
      <xsl:attribute name="found" select="../@id"/>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>~/t/ftemp $


-- Public XSLT, XSL-FO, UBL and code list classes in Europe -- Oct 2012 Contact us for world-wide XML consulting and instructor-led training Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords