[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: Wed, 22 Aug 2012 13:29:40 -0400

At 2012-08-22 12:21 -0400, Wendell Piez wrote:
Hi again,

On 8/22/2012 10:42 AM, G. Ken Holman wrote:
Ah, forgive my oversight. I missed the "all-ness" of the requirement.

I haven't yet worked out how your various solutions work so I can't
yet consider how I might make these changes.

Sounds like Wendell is on the right track and that his work would lead to a more successful result since I missed such an important requirement in my haste.

Hey no fair! Especially since I didn't address that (or any) requirement directly. I only offered a way to refactor that isolates the matching test in one place (a function), where it can be understood and adjusted more easily.

Ha! Okay ... I'll take back the problem for a solution.


Mind you, having finally solved the issue (I think! Please tell me if I'm wrong!), I ended up not feeling strongly about using a function anywhere.

Richard, given that groups are defined in the groups.xml file, where is the flexibility needed for a function? Isn't the flexibility found in the groups.xml file?

I've taken what I think is a very time-saving approach, first finding the groups that are in the area, and only then walking over the area alarms comparing each to the found groups. That way I'm not re-finding the found groups for every alarm, which I think is what the function was doing.

I just can't figure out where function flexibility is needed ... I cannot see what needs to be tweaked that isn't simply in the groups.xml file.

A transcript is below of what I hope is helpful this time around. Please let me know where I may have misunderstood the requirements, especially for flexibility. I hope the embedded comments are helpful.

Apologies, again, for having jumped to the wrong solution in my earlier post.

. . . . . . . . . . . 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"/>
      <alarm equipment="3 Alarm"/>
      <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:output indent="yes"/>

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

<xsl:template match="area">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:variable name="thisAlarmArea" select="."/>
    <!--find all alarm groups where every group member is in the alarm-->
    <xsl:variable name="foundGroups" select="$groupedAlarms
        (:there can be no mis-matches:)
        [every $groupAlarm in alarm satisfies
             some $areaAlarm in $thisAlarmArea/alarm
                satisfies matches( $areaAlarm/@equipment,
                                   concat('^',$groupAlarm/@equipment,'$'))]"/>
    <!--walk through the area alarms, annotating those that are found in
        the found groups (ignoring those groups where not all were found)-->
    <xsl:for-each select="alarm">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:variable name="thisAreaAlarm" select="."/>
        <!--only bother looking through the found groups, not others-->
        <xsl:for-each select="$foundGroups">
          <xsl:for-each select="alarm[matches($thisAreaAlarm/@equipment,
                                              concat('^',@equipment,'$'))]">
            <!--found the grouped alarm that the area alarm matches-->
            <xsl:variable name="thisGroupedAlarm" select="."/>
            <xsl:attribute name="found" select="../@id"/>
            <xsl:analyze-string select="$thisAreaAlarm/@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:for-each>
      </xsl:copy>
    </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