xsl-list
[Top] [All Lists]

Re: [xsl] Re: [xsl 1.0] howto merge branches by name

2011-12-14 15:26:21
Sorry, I didn't finish my critique of your approach.

At 2011-12-14 22:37 +0200, Adrian Herscu wrote:
        <xsl:template match="suite">
                <suite name="{@name}">
                        <xsl:for-each select="//case">
<xsl:if test="generate-id() = generate-id(key('cases',concat(../@name, '.', @name))[1])">
                                        <xsl:apply-templates select="." />
                                </xsl:if>
                        </xsl:for-each>
                </suite>
        </xsl:template>

Your problem is in your key lookup above: you are looking at each case with its respective suite parent, rather than the unique suite parent that was determined in the previous step. The processor is finding multiple occurrences of the first of a case within its parent, rather than the first of a case in all parents of the same suite attribute.

Below is your code modified only to fix the above, and then my version of your code. Again, I'm guessing at your input content. And I'm assuming you aren't trying to group unique procedures by their attribute.

I hope this helps.

. . . . . . . . . Ken

p.s. please forgive me for misreading the original subject line that indicated you were working with XSLT 1.0


t:\ftemp>type adrian.xml
<?xml version="1.0" encoding="UTF-8"?>
<test>
  <suite name="A">
    <case name="A">
      <procedure name="A" />
    </case>
  </suite>
  <suite name="A">
    <case name="A">
      <procedure name="B" />
    </case>
  </suite>
  <suite name="A">
    <case name="B">
      <procedure name="A" />
    </case>
  </suite>
  <suite name="C">
    <case name="A">
      <procedure name="A" />
    </case>
  </suite>
  <suite name="C">
    <case name="A">
      <procedure name="B" />
    </case>
  </suite>
  <suite name="C">
    <case name="B">
      <procedure name="A" />
    </case>
  </suite>
</test>

t:\ftemp>call xslt adrian.xml adrian-modified.xsl
<?xml version="1.0" encoding="utf-8"?>
<test>
   <suite name="A">
      <case name="A">
         <procedure name="A"/>
         <procedure name="B"/>
      </case>
      <case name="B">
         <procedure name="A"/>
      </case>
   </suite>
   <suite name="C">
      <case name="A">
         <procedure name="A"/>
         <procedure name="B"/>
      </case>
      <case name="B">
         <procedure name="A"/>
      </case>
   </suite>
</test>
t:\ftemp>call xslt adrian.xml adrian-ken.xsl
<?xml version="1.0" encoding="utf-8"?>
<test>
   <suite name="A">
      <case name="A">
         <procedure name="A"/>
         <procedure name="B"/>
      </case>
      <case name="B">
         <procedure name="A"/>
      </case>
   </suite>
   <suite name="C">
      <case name="A">
         <procedure name="A"/>
         <procedure name="B"/>
      </case>
      <case name="B">
         <procedure name="A"/>
      </case>
   </suite>
</test>
t:\ftemp>type adrian-modified.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output indent="yes"/>

        <xsl:key name="suites" match="//suite" use="@name" />
<xsl:key name="cases" match="//case" use="concat(../@name, '.', @name)" />

        <xsl:template match="/">
                <xsl:apply-templates />
        </xsl:template>

        <xsl:template match="test">
                <test>
                        <xsl:for-each select="//suite">
<xsl:if test="generate-id() = generate-id(key('suites',@name)[1])">
                                        <xsl:apply-templates select="." />
                                </xsl:if>
                        </xsl:for-each>
                </test>
        </xsl:template>

        <xsl:template match="suite">
                <suite name="{@name}">
                  <xsl:variable name="suite-name" select="@name"/>
                        <xsl:for-each select="//case">
                                <xsl:if test="generate-id() =
generate-id(key('cases',concat($suite-name, '.', @name))[1])">
                                        <xsl:apply-templates select="." />
                                </xsl:if>
                        </xsl:for-each>
                </suite>
        </xsl:template>

        <xsl:template match="case">
                <case name="{@name}">
<xsl:copy-of select="key('cases',concat(../@name, '.', @name))/
                                       procedure"/>
                </case>
        </xsl:template>
</xsl:stylesheet>
t:\ftemp>type adrian-ken.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output indent="yes"/>

  <xsl:key name="suites" match="suite" use="@name" />
  <xsl:key name="cases" match="case" use="concat(../@name, '&#xd;', @name)" />

  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="test">
    <test>
      <xsl:apply-templates select="suite[generate-id() =
                                  generate-id(key('suites',@name)[1])]"/>
    </test>
  </xsl:template>

  <xsl:template match="suite">
    <suite name="{@name}">
      <xsl:variable name="suite-name" select="@name"/>
      <xsl:apply-templates select="//case[generate-id() =
          generate-id(key('cases',concat($suite-name, '&#xd;', @name))[1])]"/>
    </suite>
  </xsl:template>

  <xsl:template match="case">
    <case name="{@name}">
      <xsl:copy-of select="key('cases',concat(../@name, '&#xd;', @name))/
                           procedure"/>
    </case>
  </xsl:template>

</xsl:stylesheet>
t:\ftemp>

--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/uoui9h
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/
G. Ken Holman                   mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>