[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 15:01:51 -0400

At 2012-08-21 16:28 +0000, Kerry, Richard wrote:
Ok, Thanks Ken,
I've got both of these solutions working as given, with the results as stated.

However ....

They don't quite implement the spec as given.

An oversight due to my haste. I'm back now from my errand.


> The pattern must be found in its entirety (ie LAR 51 doesn't find "group-2" as "2 Alarm" is not present).

Both the solutions provided are indicating group-2 is present for LAR 51 but this is not so as "2 Alarm" is not actually present there.

But I see group-2 as:


<group id="group-2">
<alarm equipment="1 Alarm"/>
<alarm equipment="2 Alarm"/>
<alarm equipment="3 Alarm"/>
</group>

And I see LAR 51 as:

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

So, then, I see two members of group-2 are in LAR 51.

The pattern given must match in its entirety for a match to be declared.
(There may be a further subsidiary issue if I find I need to be able to match "subset" groups, ie where one defined match group is a subset of another but the smaller group may only be matched if the larger one doesn't - ignore this for now; I'll need to work out whether it's necessary once I've got something nearer to the full data.)

The simple fix for a complete string match would be to add two characters to the regex= and a function to match() argument. Results below. But it does highlight a worry that your equipment= values might contain regex pattern characters that should be escaped.


> I can help if you don't mind using the following notation using grouping parentheses:
That's no problem - I can adapt the group spec format as I'll be writing that based on a brief visual analysis of the main data.

Then just be sensitive to the presence of significant characters.


I hope the example below allays the earlier concerns.

As I haven't yet had the final data to work on and haven't yet determined exactly how I want the results organized I am interested at the moment in working out the principles of the task and I'll sort out the details later.

Good luck, Richard! (and sorry about using the wrong name ... again, I was rushing out the door)


. . . . . . . . . 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>
<group id="group-3">
<alarm equipment="MDU X([0-9]{4}) Relay fail"/>
<alarm equipment="MDU X([0-9]{4}) Fuse fail"/>
</group></groups>~/t/ftemp $
~/t/ftemp $ cat richard.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 richard.xml richard.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" found="group-3" found-group="1" found-key="2301"/>
<alarm equipment="2 Alarm" found="group-2"/>
<alarm equipment="MDU X2301 Fuse fail" found="group-3" found-group="2" found-key="2301"/>
</area>
<area name="LAR 41">
<alarm equipment="MDU X4107 Relay fail" found="group-3" found-group="1" found-key="4107"/>
<alarm equipment="MDU X4107 Fuse fail" found="group-3" found-group="2" found-key="4107"/>
<alarm equipment="MDU X4108 Relay fail" found="group-3" found-group="1" found-key="4108"/>
<alarm equipment="MDU X4108 Fuse fail" found="group-3" found-group="2" found-key="4108"/>
<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" found="group-3" found-group="1" found-key="6311"/>
<alarm equipment="MDU X6311 Fuse fail" found="group-3" found-group="2" found-key="6311"/>
</area>
</areas>~/t/ftemp $
~/t/ftemp $ cat richard.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">


<xsl:variable name="groupedAlarms"
              select="doc('groups.xml')/groups/group/alarm"/>

<xsl:template match="alarm">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:variable name="thisAlarm" select="."/>
    <xsl:for-each select="$groupedAlarms
                          [matches($thisAlarm/@equipment,
                                   concat('^',@equipment,'$'))]">
      <xsl:attribute name="found" select="../@id"/>
      <xsl:variable name="thisGroupedAlarm" select="."/>
      <xsl:analyze-string select="$thisAlarm/@equipment"
                          regex="^{$thisGroupedAlarm/@equipment}$">
        <xsl:matching-substring>
          <xsl:for-each select="regex-group(1)[normalize-space(.)]">
            <xsl:attribute name="found-group">
              <xsl:number select="$thisGroupedAlarm"/>
            </xsl:attribute>
            <xsl:attribute name="found-key" select="."/>
          </xsl:for-each>
        </xsl:matching-substring>
      </xsl:analyze-string>
    </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 $
~/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