xsl-list
[Top] [All Lists]

Re: [xsl] Newbie needs XSL help, 1-to-many xform, multi-value data items

2010-12-14 13:01:53
At 2010-12-14 18:41 +0000, thehulk(_at_)comcast(_dot_)net wrote:
My problem is, i do not know how to create multiple elements from a single element with several multi-value child elements.

By walking the tree as many times as you need.

The order of the child nodes has meaning that is managed by the Notes application, so that the set of multi-value data items has the meaning of one two-dimensional array of data.

I've assumed you always have exactly as many cities as you have names.

If my application puts three people and their cities into one Notes document, Domino gives me XML much like this (simplified of course):
...
I need to turn that one "MyRecord" element into multiple "ATransaction" elements (three of them, in this case). Each of them has certain data from "MyRecord", along with only one value from each of the multi-child elements "MyNames" and "MyCities".
...

Not a problem. In the solution below I use the position of each of the names as my counter and then walk the information pulling out the repeated information at the location of the counter in each loop.

I hope this helps.

. . . . . . . . Ken

T:\ftemp>type hulk.xml
 <MyRecord>
   <BranchID> 12345 </BranchID>
   <RecordDate> November 3, 2009 </RecordDate>
   <MyNames>
      <Name> John Doe </Name>
      <Name> Rachel Roe </Name>
      <Name> Biff Jones </Name>
   </MyNames>
   <MyCities>
      <City> Kalamazoo </City>
      <City> Katmandu </City>
      <City> Timbuktu </City>
   </MyCities>
 </MyRecord>
T:\ftemp>xslt hulk.xml hulk.xsl
<?xml version="1.0" encoding="utf-8"?>
<Transactions>
   <ATransaction>
      <Branch> 12345 </Branch>
      <When> November 3, 2009 </When>
      <Who> John Doe </Who>
      <Where> Kalamazoo </Where>
   </ATransaction>
   <ATransaction>
      <Branch> 12345 </Branch>
      <When> November 3, 2009 </When>
      <Who> Rachel Roe </Who>
      <Where> Katmandu </Where>
   </ATransaction>
   <ATransaction>
      <Branch> 12345 </Branch>
      <When> November 3, 2009 </When>
      <Who> Biff Jones </Who>
      <Where> Timbuktu </Where>
   </ATransaction>
</Transactions>
T:\ftemp>type hulk.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="MyRecord">
  <Transactions>
    <xsl:variable name="this" select="."/>
    <xsl:for-each select="MyNames/Name">
      <xsl:variable name="pos" select="position()"/>
      <xsl:for-each select="$this">
        <ATransaction>
          <Branch><xsl:apply-templates select="BranchID/node()"/></Branch>
          <When><xsl:apply-templates select="RecordDate/node()"/></When>
          <Who>
            <xsl:apply-templates select="MyNames/Name[$pos]/node()"/>
          </Who>
          <Where>
            <xsl:apply-templates select="MyCities/City[$pos]/node()"/>
          </Where>
        </ATransaction>
      </xsl:for-each>
    </xsl:for-each>
  </Transactions>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>


--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
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>