XML to XML looping problem (newbie)

Discussion in 'XML' started by MKline, Dec 13, 2006.

  1. MKline

    MKline Guest

    Hello,

    I have XML source to map to XML output. I am having trouble getting
    the XSL to loop through the entire XML until it reaches the end. This
    is an ASN for a Customer and I am getting the first ASN in my output,
    but can't get it to continue to next <CUSTASN> and so on until the end.

    Suggestions would be greatly appreciated.
    Thanks

    Source is like this and repeats starting with <CUSTASN> until the end
    of all shipment data:

    <?xml version="1.0" encoding="UTF-8"?>
    <CUSTASN>
    <SUPPID>8840 </SUPPID>
    <DUNSNO>141 </DUNSNO>
    <PLTNME>ACUNA 141 </PLTNME>
    <GRWGT>32</GRWGT>
    <CONTDE>CTN90</CONTDE>
    <CQTY>1</CQTY>
    <SHPDTE>2006-12-07</SHPDTE>
    <F3WRBT>3747040 00 </F3WRBT>
    <USR20C>I202723 </USR20C>
    <TODAY>2006-12-12</TODAY>
    <PONO>600777 </PONO>
    <POLIN>89 </POLIN>
    <CPART>213256 </CPART>
    <ENGREV>0 </ENGREV>
    <UOFMM>EA </UOFMM>
    <RELNO>1205060924</RELNO>
    <RLSDTE>2006-11-21</RLSDTE>
    <QTYSHP>400</QTYSHP>
    <SHPDTE01>2006-12-07</SHPDTE01>
    </CUSTASN>
    .......repeats with next <CUSTASN> until end of XML.....

    Target is this format (based on Customer requirement):

    <?xml version="1.0" encoding="UTF-8"?>
    <MXSCSHIPMENT>
    <SUPPLIER></SUPPLIER>
    <SHIPFROM></SHIPFROM>
    <FACILITY></FACILITY>
    <SHIPTO></SHIPTO>

    <CREATEDATE></CREATEDATE>
    <NUMASN></NUMASN>
    <!-- Must match # of ASN Elements included in transaction -->
    <ASN>
    <!-- One or More ASN Elements must be present -->
    <ASNNUMBER/>
    <!-- Include ASN Number or if null, system will generate -->
    <ASNDATE></ASNDATE>
    <CONTAINERTYPE></CONTAINERTYPE>
    <!-- Must contain valid MxSC Code of type CONTAINERTYPE -->
    <NUMCONTAINERS></NUMCONTAINERS>
    <LENGTH></LENGTH>
    <WIDTH></WIDTH>
    <HEIGHT></HEIGHT>
    <GROSSWEIGHT></GROSSWEIGHT>

    <LENGTH_UOM></LENGTH_UOM>
    <!-- Must contain valid MxSC Code of type LENGTHUOM -->
    <WIDTH_UOM></WIDTH_UOM>
    <!-- Must contain valid MxSC Code of type LENGTHUOM -->
    <HEIGHT_UOM></HEIGHT_UOM>
    <!-- Must contain valid MxSC Code of type LENGTHUOM -->
    <GROSSWEIGHT_UOM></GROSSWEIGHT_UOM>
    <!-- Must contain valid MxSC Code of type WEIGHTUOM -->

    <ROUTING></ROUTING>
    <PAYMENTMETHOD></PAYMENTMETHOD>
    <!-- Must contain valid MxSC Code of type PAYMENTTYPE-->
    <TRACKINGNUMBER></TRACKINGNUMBER>
    <PACKINGSLIP></PACKINGSLIP>

    <NUMSHIPMENTS></NUMSHIPMENTS>
    <SHIPMENT>
    <PONUMBER></PONUMBER>
    <POLINE></POLINE>
    <ITEM></ITEM>
    <REVISION></REVISION>
    <UOM></UOM>
    <RELEASEDATE></RELEASEDATE>
    <!-- Either this or ReleaseNum are required -->
    <RELEASENUM>4</RELEASENUM>
    <!-- Customer won't use ReleaseNum -->
    <SHIPDATE></SHIPDATE>
    <SHIPQUANTITY></SHIPQUANTITY>
    </SHIPMENT>
    <SHIPMENT>
    <PONUMBER></PONUMBER>
    <POLINE></POLINE>
    <ITEM></ITEM>
    <REVISION></REVISION>
    <UOM></UOM>
    <RELEASEDATE></RELEASEDATE>
    <!-- Either this or ReleaseNum are required -->
    <RELEASENUM></RELEASENUM>
    <!-- Customer won't use ReleaseNum -->
    <SHIPDATE></SHIPDATE>
    <SHIPQUANTITY></SHIPQUANTITY>
    </SHIPMENT>
    </ASN>
    </MXSCSHIPMENT>

    XSL:

    <?xml version='1.0' ?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <MXSCSHIPMENT>
    <SUPPLIER>
    <xsl:value-of select="s:sequence/CUSTASN/SUPPID"/>
    </SUPPLIER>
    <SHIPFROM>
    <xsl:value-of select="s:sequence/CUSTASN/SUPPID"/>
    </SHIPFROM>
    <FACILITY>
    <xsl:value-of select="s:sequence/CUSTASN/PLTNME"/>
    </FACILITY>
    <SHIPTO>
    <xsl:value-of select="s:sequence/CUSTASN/DUNSNO"/>
    </SHIPTO>
    <CREATEDATE>
    <xsl:value-of select="s:sequence/CUSTASN/TODAY"/>
    </CREATEDATE>
    <NUMASN>
    <xsl:value-of select="count(s:sequence/CUSTASN/USR20C)"/>
    </NUMASN>
    <ASN>
    <ASNNUMBER>
    <xsl:value-of select="s:sequence/CUSTASN/USR20C"/>
    </ASNNUMBER>
    <CONTAINERTYPE>
    <xsl:value-of select="s:sequence/CUSTASN/CONTDE"/>
    </CONTAINERTYPE>
    <NUMCONTAINERS>
    <xsl:value-of select="s:sequence/CUSTASN/CQTY"/>
    </NUMCONTAINERS>
    <GROSSWEIGHT>
    <xsl:value-of select="s:sequence/CUSTASN/GRWGT"/>
    </GROSSWEIGHT>
    <GROSSWEIGHT_UOM>
    <xsl:value-of select="'LB'"/>
    </GROSSWEIGHT_UOM>
    <PACKINGSLIP>
    <xsl:value-of select="s:sequence/CUSTASN/F3WRBT"/>
    </PACKINGSLIP>
    <NUMSHIPMENTS>
    <xsl:value-of select="count(s:sequence/CUSTASN/F3WRBT)"/>
    </NUMSHIPMENTS>
    <SHIPMENT>
    <PONUMBER>
    <xsl:value-of select="s:sequence/CUSTASN/PONO"/>
    </PONUMBER>
    <POLINE>
    <xsl:value-of select="s:sequence/CUSTASN/POLIN"/>
    </POLINE>
    <ITEM>
    <xsl:value-of select="s:sequence/CUSTASN/CPART"/>
    </ITEM>
    <REVISION>
    <xsl:value-of select="s:sequence/CUSTASN/ENGREV"/>
    </REVISION>
    <UOM>
    <xsl:value-of select="s:sequence/CUSTASN/UOFMM"/>
    </UOM>
    <RELEASEDATE>
    <xsl:value-of select="s:sequence/CUSTASN/RLSDTE"/>
    </RELEASEDATE>
    <SHIPDATE>
    <xsl:value-of select="s:sequence/CUSTASN/SHPDTE"/>
    </SHIPDATE>
    <SHIPQUANTITY>
    <xsl:value-of select="s:sequence/CUSTASN/QTYSHP"/>
    </SHIPQUANTITY>
    </SHIPMENT>
    </ASN>

    </MXSCSHIPMENT>
    </xsl:template>
    </xsl:stylesheet>
     
    MKline, Dec 13, 2006
    #1
    1. Advertisements

  2. MKline

    Andy Dingley Guest

    Read XSLT tutorials until you start thinking about declarative
    languages, not procedural or "looping" techniques. That's quite
    probably not necessary (although you could do it that way)
    [...]
    This isn't well-formed XML, as-described.

    XML only permits a single root element. If <CUSTASN> is repeated, then
    that isn't well-formed XML and you can't use XSLT on it.

    Fix the well-formedness of your XML first. Maybe by adding an external
    root element. If you can't do this (maybe you just receive this junk
    from an ignorant supplier) then there's a DTD and entity based hack to
    work around it -- search the newsgroup.
     
    Andy Dingley, Dec 13, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.