Re: [xsl] Identity Transform Grouping Question

Subject: Re: [xsl] Identity Transform Grouping Question
From: <ethan.kalfus@xxxxxxx>
Date: Thu, 21 Oct 2004 15:19:53 -0400
Hi Anton,

<<Yes: that was a bug. In template match="city", the grouping of the office
elements included all offices in the current country (rather than the current
city).
Here's the fixed template (added "cities/city=current()" to the predicate):

<xsl:template match="city">
<city>
<name><xsl:value-of select="."/></name>
<offices>
<!-- group 'office' elements *located in this city* by their english name -->
<xsl:apply-templates select="ancestor::offices/office
[cities/city=current() and count(.|key('offices',
concat(ancestor::country/@name,'-',names/name[@lang='en']))[1])=1]">
<xsl:sort select="names/name[@lang='en']"/>
</xsl:apply-templates>
</offices>
</city>
</xsl:template>
>>

   Unfortunately, that fixed the previous problem, but I've now noticed the
opposite effect -- offices with the same name in different cities are being
grouped together.  I think the problem is that the "offices" key needs to
include the <city> node to form the proper grouping (office within city within
country).  I tried changing the key as follows:
<xsl:key name="offices" match="office"
use="concat(ancestor::country/@name,'-',cities/city,'-',names/name[@lang='en'
])"/>
and changing the city and office templates as follows:
   city:
<xsl:apply-templates select="ancestor::offices/office[cities/city=current()
and
count(.|key('offices',concat(ancestor::country/@name,'-',cities/city=current(
),'-',names/name[@lang='en']))[1])=1]">
<xsl:sort select="names/name[@lang='en']"/>

   office:
<xsl:apply-templates
select="key('offices',concat(ancestor::country/@name,'-',cities/city=current(
),'-',names/name[@lang='en']))" mode="location"/>

   These changes seemed to fix my above problem, except that the result tree
is missing all of the <address> and <phone> nodes.  I'm not sure how my
changes affected this.

   To illustrate the latest problem, I modified my last source example to
change the city name for <office id="BR6"> from "London" to "New Castle"
(complete source tree below for your convenience):

<?xml version="1.0" encoding="UTF-8"?>
<locations version="1.0">
   <divisions>
      <division id="B">
         <regions>
            <region name="pacific">
               <countries>
                  <country name="China">
                     <offices>
                        <office id="BR5">
                           <names>
                              <name lang='en'>Branch 5</name>
                           </names>
                           <address>
                              <line>China World Tower 1</line>
                              <line>1 Jian Guo Men Wai Avenue</line>
                              <line>Beijing</line>
                           </address>
                           <cities>
                              <city>Beijing</city>
                           </cities>
                           <phone/>
                        </office>
                     </offices>
                  </country>
               </countries>
            </region>
         </regions>
      </division>
      <division id="A">
         <regions>
            <region name="europe">
               <countries>
                  <country name="Germany">
                     <offices>
                        <office id="BR3">
                           <names>
                              <name lang='en'>Branch 3</name>
                           </names>
                           <address>
                              <line>P.O. Box 1210</line>
                              <line>Frankfurt, Germany</line>
                           </address>
                           <cities>
                              <city>Frankfurt</city>
                           </cities>
                           <phone>+49-55-5555 5555</phone>
                        </office>
                     </offices>
                  </country>
                  <country name="England">
                     <offices>
                        <office id="BR6">
                           <names>
                              <name lang='en'>Branch 4</name>
                           </names>
                           <address>
                              <line>26 Abbey Lane</line>
                              <line>New Castle</line>
                           </address>
                           <cities>
                              <city>New Castle</city>
                           </cities>
                           <phone>+44-22-2222 2222</phone>
                        </office>
                        <office id="BR5">
                           <names>
                              <name lang='en'>AAAAA Branch 4</name>
                           </names>
                           <address>
                              <line>7 Kings Highway</line>
                              <line>London</line>
                           </address>
                           <cities>
                              <city>London</city>
                           </cities>
                           <phone>+44-99-9999 9999</phone>
                        </office>
                        <office id="BR7">
                           <names>
                              <name lang='en'>Branch 4</name>
                           </names>
                           <address>
                              <line>22 Abbey Lane</line>
                              <line>London</line>
                           </address>
                           <cities>
                              <city>London</city>
                           </cities>
                           <phone>+44-55-555 5555</phone>
                        </office>
                     </offices>
                  </country>
               </countries>
            </region>
            <region name="americas">
               <countries>
                  <country name="United States">
                     <offices>
                        <office id="HO">
                           <names>
                              <name lang='en'>Home Office</name>
                           </names>
                           <address>
                              <line>P.O. Box 1234</line>
                              <line>New York, NY  11111</line>
                           </address>
                           <cities>
                              <city>New York</city>
                           </cities>
                           <phone>(212) 123-4567</phone>
                        </office>
                        <office id="BR1">
                           <names>
                              <name lang='en'>Branch 1</name>
                           </names>
                           <address>
                              <line>999 Main Street</line>
                              <line>Suite 1200</line>
                              <line>Miami, FL  22222</line>
                           </address>
                           <cities>
                              <city>Miami</city>
                           </cities>
                           <phone>777-7777</phone>
                        </office>
                     </offices>
                  </country>
                  <country name="Canada">
                     <offices>
                        <office id="Branch 2">
                           <names>
                              <name lang='en'>Canadian Branch</name>
                           </names>
                           <address>
                              <line>1 Prince Edward Boulevard</line>
                              <line>5th Floor</line>
                              <line>Room 10</line>
                              <line>Calgary, Alberta</line>
                           </address>
                           <cities>
                              <city>Calgary</city>
                           </cities>
                           <phone>(888) 888-8888</phone>
                        </office>
                     </offices>
                  </country>
               </countries>
            </region>
         </regions>
      </division>
   </divisions>
</locations>

   Here's the affected result tree fragment (location BR7 should follow
location BR5):

<region name="europe">
   <countries>
      <country name="England">
         <cities>
            <city>
               <name>London</name>
               <offices>
                  <office>
                     <names>
                        <name lang="en">AAAAA Branch 4</name>
                     </names>
                     <location id="BR5">
                        <address>
                           <line>7 Kings Highway</line>
                           <line>London</line>
                        </address>
                        <phone>+44-99-9999 9999</phone>
                     </location>
                  </office>
               </offices>
            </city>
            <city>
               <name>New Castle</name>
               <offices>
                  <office>
                     <names>
                        <name lang="en">Branch 4</name>
                     </names>
                     <location id="BR6">
                        <address>
                           <line>26 Abbey Lane</line>
                           <line>New Castle</line>
                        </address>
                        <phone>+44-22-2222 2222</phone>
                     </location>
                     <location id="BR7">
                        <address>
                           <line>22 Abbey Lane</line>
                           <line>London</line>
                        </address>
                        <phone>+44-55-555 5555</phone>
                     </location>
                  </office>
               </offices>
            </city>
         </cities>
      </country>

   Just to make sure I've communicated this clearly, the grouping should be
done within the desired sort order for the entire source tree, which is as
follows (least granular to most granular): division/@id, region/@name,
country/@name, city, office/names/name[@lang='en'], address.

   Thanks!

Visit our website at http://www.ubs.com

This message contains confidential information and is intended only
for the individual named.  If you are not the named addressee you
should not disseminate, distribute or copy this e-mail.  Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses.  The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission.  If
verification is required please request a hard-copy version.  This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.

Current Thread