xsl-list
[Top] [All Lists]

Re: best-technique for selective group and merge?

2004-03-02 10:44:36
At 2004-03-02 08:43 -0800, James A. Robinson wrote:
The feature can contain multiple configs,
each of which I want to merge across features:
...
I want to be able to select /config_datastore/site/* and perform selective
grouping (on feature @name) and merging (on the feature/config elements)
to return:
...

I found I could wrap my head around this problem using variables instead of keys. An example is below giving your desired results. Note how I take advantage of the node-set comparison of a single value to a set of values to get from all configs only those whose feature's name is in a set of names.

I hope this helps.

.................... Ken


T:\ftemp>type jim.xml
<?xml version="1.0"?>
  <config_datastore>
    <feature name="a">
      <config id="1">
        <param id="x">x</param>
      </config>
      <config id="2">
        <param id="xx">xx</param>
      </config>
      <config id="3">
        <param id="xxx">xxx</param>
      </config>
    </feature>
    <feature name="b">
      <config id="1">
        <param id="y">y</param>
      </config>
      <config id="2">
        <param id="yy">yy</param>
      </config>
    </feature>
    <feature name="c">
      <config id="1">
        <param id="z">z</param>
      </config>
      <config id="2">
        <param id="zz">zz</param>
      </config>
    </feature>
    <site>
      <has_feature name="a"/>
      <has_feature name="c"/>
    </site>
  </config_datastore>

T:\ftemp>type jim.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <result>
    <xsl:variable name="configs"
                  
select="/*/feature[(_at_)name=/*/site/has_feature/@name]/config"/>
    <xsl:for-each select="$configs">
      <xsl:if test="generate-id(.)=
                    generate-id($configs[(_at_)id=current()/@id])">
        <config id="{(_at_)id}">
          <xsl:copy-of select="$configs[(_at_)id=current()/@id]/*"/>
        </config>
      </xsl:if>
    </xsl:for-each>
  </result>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>saxon jim.xml jim.xsl
<?xml version="1.0" encoding="utf-8"?>
<result>
   <config id="1">
      <param id="x">x</param>
      <param id="z">z</param>
   </config>
   <config id="2">
      <param id="xx">xx</param>
      <param id="zz">zz</param>
   </config>
   <config id="3">
      <param id="xxx">xxx</param>
   </config>
</result>
T:\ftemp>rem Done!



--
US XSL training: Washington,DC March 15; San Francisco,CA March 22
World-wide on-site corporate, government & user group XML training
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list