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: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 21 Aug 2012 13:06:02 -0400
Dear Richard,

Ken has actually demonstrated everything you need to know in the problem you pose. Indeed, since the difference between his solution 1 and solution 2 is effectively the matching criterion -- what qualifies an entry as "found" -- then comparing the two should show you what you need to generalize to any matching criteria you may have (comparing two or more values, etc.).

It's true that in giving you complete solutions, he hasn't separated the solution to the problem as posed from the incidentals that go with making it work. But this makes good homework. (By not breaking it down and explaining it he's actually expressing his confidence in you, I'd say.) So I suggest taking the next step by picking one of his solutions and tinkering with it. (Undoubtedly you are already doing this.) Do feel free while you are doing so to come back to the list with more questions.

If it were my problem I might encapsulate the test for a match into a stylesheet function, both for clarity and maintenance. A first step could be to do this with Ken's solution 1, something like

<xsl:function name="x:my-group" as="element(group)*">
  <!-- returns any 'group' elements in groups.xml with child
       nodes matching $me/@equipment by the 'grouped' key -->
  <xsl:param name="me" as="element(alarm)"/>
  <xsl:sequence
    select="key('grouped',$me/@equipment,doc('groups.xml'))
            /parent::group"/>
</xsl:function>

(I did make a slight change from Ken's logic: this fetches the 'group' element not the 'alarm' element inside it.)

then

<xsl:template match="alarm">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each select="x:my-group(.)">
      <xsl:attribute name="found" select="@id"/>
    </xsl:for-each>
  </xsl:copy>
</xsl:template>

Once the logic is embedded in the function you can extend or refine it as much as you like. Nor do you have to use a key, as Ken did (and then didn't in his second solution): what the function does is return the elements from your lookup document that correspond with the 'alarm' element being matched, and any way it does that is fine.

Cheers,
Wendell

--
======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread