[xsl] Conditional Merge - what am I missing?

Subject: [xsl] Conditional Merge - what am I missing?
From: Nate Austin <naustin@xxxxxxxxxx>
Date: Mon, 7 Jan 2002 12:26:08 -0600
Hello all.  I've got two bits of xml - one is a stub that basically defines
the layout (it has no text data in it), the other could have more or less
elements and contains text (I'll refer to that one as the data tree).  The
two will have a similar structure.  If the stub contains an element, then
all corresponding elements should be copied from the data tree.  The same
should be done for the sub-elements.  If there are no corresponding elements
in the data tree, the stub should be copied out itself.  Here's what I've
come up with:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="MergeDataXML">
    <xsl:apply-templates select="DataStruct[@Id = 'Stub']"/>
  </xsl:template>

  <xsl:template match="DataStruct">
    <xsl:copy>
      <xsl:attribute name="Id">
        <xsl:value-of select="//DataStruct[@Id != 'Stub']/@Id"/>
      </xsl:attribute>
      <xsl:apply-templates select="*">
        <xsl:with-param name="context" select="//DataStruct[@Id !=
'Stub']"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="context"/>
    <xsl:variable name="currNodeName" select="name()"/>
    <xsl:choose>
      <xsl:when test="count($context/child::*[name() = $currNodeName]) &gt;=
1">
        <xsl:for-each select="$context/child::*[name() = $currNodeName]">
          <xsl:copy>
            <xsl:apply-templates>
              <xsl:with-param name="context" select="current()"/>
            </xsl:apply-templates>
          </xsl:copy>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>


<MergeDataXML>
  <DataStruct Id="Stub">
    <CustData>
      <CustName/>
      <FedId/>
      <RelatedParty>
        <CustName/>
        <FedId/>
      </RelatedParty>
    </CustData>
  </DataStruct>
  <DataStruct Id="DataTree">
    <CustData>
      <CustName>Some Name</CustName>
      <FedId>Some Id</FedId>
      <RelatedParty>
        <CustName>RPName</CustName>
        <FedId>RPId</FedId>
      </RelatedParty>
      <RelatedParty>
        <CustName>RPName2</CustName>
        <FedId>RPId2</FedId>
      </RelatedParty>
    </CustData>
  </DataStruct>
</MergeDataXML>

The results I'm getting are strange.  The <RelatedParty> structures are
being repeated twice for each of them.  Also, if any element is not included
in the data tree it is being excluded completely.  If it isn't included in
the stub it is being included anyways.  This is the reverse of what I want,
but looking at my XSLT it doesn't appear that the logic should be reversed.
Can anyone shed some light on what I'm missing here?

Thanks,

Nate Austin
International Decision Systems
(612)277-2523
naustin@xxxxxxxxxx


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


Current Thread