[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: Tue, 21 Aug 2012 16:28:34 +0000

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.

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


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


>><alarm equipment="MDU X2301 Relay fail" found="group-3" found-group="1"
found-key="2301" />
>><alarm equipment="MDU X2301 Fuse fail" found="group-3" found-group="1"
found-key="2301" />
>
>Wouldn't that be found-group="2"?

Well, that would depend on exactly how I defined found-group.  I probably
didn't state anything in particular particularly clearly.  What I was thinking
was that within each "area" element a regex pattern could be found more than
once, and each complete instance would be marked (in found-group) by a common
incrementing id.  Though this is actually doing more or less the same as the
found-key, but with much smaller numbers.  It may therefore be more useful to
have what you've provided, which I think is that found-group indicates which
element of the match group is associated with each alarm element.  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.


Regards,
Richard.



(not Robert)





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 15:48
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx; xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Pattern Matching in XSl - find groups defined in one  Xml
in another Xml.

This turned out to be easier than I thought, so I could do it before leaving.

At 2012-08-21 13:38 +0000, Kerry, Richard wrote:
>- Supplementary question.
>Can this be extended to find patterns defined using regular expressions ?
>eg add to the above pattern file :
><group id="group-3">
><alarm equipment="MDU X[0-9]{4} Relay fail"/>
><alarm equipment="MDU X[0-9]{4} Fuse fail"/>
></group>

I can help if you don't mind using the following notation using
grouping parentheses:

<group id="group-3">
<alarm equipment="MDU X([0-9]{4}) Relay fail"/>
<alarm equipment="MDU X([0-9]{4}) Fuse fail"/>
</group>

>The result would be changed to add the following :
>
><area name="LAR 23">
><alarm equipment="MDU X2301 Relay fail" found="group-3"
>found-group="1" found-key="2301" />
><alarm equipment="MDU X2301 Fuse fail" found="group-3"
>found-group="1" found-key="2301" />

Wouldn't that be found-group="2"?

I hope the example below 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>
<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 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" 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 robert.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,@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 $


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