[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: "Kerry, Richard" <richard.kerry@xxxxxxxx>
Date: Wed, 22 Aug 2012 13:59:51 +0000

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

Exactly, and since the group has three members that's not a match.  One "area"
needs to contain a match for all the elements in a group to count as a match.


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


Now, off to find the docs for "key" and analyze-string/matching-substring.



Appreciatively,.
Richard.




Richard Kerry
BNCS Engineer
T: +44 (0)20 82259063
M: +44 (0)7812 325518
Room EBX 301, BBC Television Centre, Wood Lane, London, W12 7RJ
richard.kerry@xxxxxxxx
uk.atos.net

This e-mail and the documents attached are confidential and intended solely
for the addressee; it may also be privileged. If you receive this e-mail in
error, please notify the sender immediately and destroy it. As its integrity
cannot be secured on the Internet, the Atos group liability cannot be
triggered for the message content. Although the sender endeavours to maintain
a computer virus-free network, the sender does not warrant that this
transmission is virus-free and will not be liable for any damages resulting
from any virus transmitted.

________________________________________
From: G. Ken Holman [g.ken.holman@xxxxxxxxx] on behalf of G. Ken Holman
[gkholman@xxxxxxxxxxxxxxxxxxxx]
Sent: 21 August 2012 20:01
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx; xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Pattern Matching in XSl - find groups defined in one   Xml
in another Xml.

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