Skip to main content
Jun 30, 2021 8:46 PM UTC

Issue with XML IMPORT with a for-each loop

I am trying to read a simple xml file that has attributes with a stylesheet, and I need to capture the data in tags that duplicate like AddressLine. For some reason I can't capture the AddressLine data.  I am getting all the other data like Zip, State, City, Name just fine, but getting spaces for the AddressLine. Here is an example of my xml file, stylesheet and cobol program:

focus3.xml (XML FILE)

<?xml version="1.0" encoding="UTF-8"?>

<focus-address Zip="78728" State="TX" City="Austin" Name="Micro Focus" Timestamp="10544627">
   <mainAddress>

      <addressAll AddressLine="8310 North Capital of Tesax Highway"/>

      <addressAll AddressLine="Building 1, Suite 155"/>

   </mainAddress>

</focus-address>

XML Stylesheet:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="">www.w3.org/.../Transform" >
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:apply-templates select="focus-address" />
</xsl:template>
<xsl:template match="focus-address">
<Focus-Address>
   <xp-TimeStamp><xsl:value-of select="@Timestamp"/></xp-TimeStamp>
   <xp-Name><xsl:value-of select="@Name"/></xp-Name>
   <xp-City><xsl:value-of select="@City"/></xp-City>
   <xp-State><xsl:value-of select="@State"/></xp-State>
   <xp-Zip><xsl:value-of select="@Zip"/></xp-Zip>
   <Mainaddress>
      <xsl:for-each select="focus-address/addressAll/AddressLine">
      <Addressall>
         <xp-Address-Line><xsl:value-of select="focus-address/addressAll/@AddressLine"/></xp-Address-Line>
      </Addressall>
      </xsl:for-each>
   </Mainaddress>
</Focus-Address>
</xsl:template>
</xsl:stylesheet>

Cobol program:

Data Division.
Working-Storage Section.
01 Focus-Address.
    02 xp-TimeStamp Pic 9(8).
    02 xp-Name Pic X(64).
    02 xp-City Pic X(32).
    02 xp-State Pic X(2).
    02 xp-Zip Pic 9(5).
    02 Mainaddress.
         20 Addressall OCCURS 1 TO 2 TIMES
                        DEPENDING ON Address-Lines.
              30 xp-Address-Line Pic X(64).
01 Done Pic X.
01 Address-lines Pic 9.
Copy "../cpy/lixmlall.cpy".

Procedure Division.
A. Display "Example-3 - Illustrate EXPORT FILE & IMPORT FILE"
                                    " with OCCURS DEPENDING".
     XML INITIALIZE.
     If Not XML-OK Go to Z.

     Move 2 to Address-Lines.

     Move Spaces to Focus-Address.

      XML IMPORT FILE
            Focus-Address
            "focus3"
            "Focus-Address"
            "example3.xsl".
     
      If Not XML-OK Go to Z.
      Display "focus3.xml imported by XML IMPORT FILE".
      Display xp-Name.
      Display xp-Address-Line(1).
      Display xp-Address-Line(2).
      Display xp-City xp-State xp-Zip.
      Display xp-TimeStamp.

       

    1 reply

    Dominique Sacre
    Rocketeer
    Jul 1, 2021 9:00 PM UTC

    I am trying to read a simple xml file that has attributes with a stylesheet, and I need to capture the data in tags that duplicate like AddressLine. For some reason I can't capture the AddressLine data.  I am getting all the other data like Zip, State, City, Name just fine, but getting spaces for the AddressLine. Here is an example of my xml file, stylesheet and cobol program:

    focus3.xml (XML FILE)

    <?xml version="1.0" encoding="UTF-8"?>

    <focus-address Zip="78728" State="TX" City="Austin" Name="Micro Focus" Timestamp="10544627">
       <mainAddress>

          <addressAll AddressLine="8310 North Capital of Tesax Highway"/>

          <addressAll AddressLine="Building 1, Suite 155"/>

       </mainAddress>

    </focus-address>

    XML Stylesheet:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="">www.w3.org/.../Transform" >
    <xsl:output method="xml" encoding="UTF-8"/>
    <xsl:template match="/">
    <xsl:apply-templates select="focus-address" />
    </xsl:template>
    <xsl:template match="focus-address">
    <Focus-Address>
       <xp-TimeStamp><xsl:value-of select="@Timestamp"/></xp-TimeStamp>
       <xp-Name><xsl:value-of select="@Name"/></xp-Name>
       <xp-City><xsl:value-of select="@City"/></xp-City>
       <xp-State><xsl:value-of select="@State"/></xp-State>
       <xp-Zip><xsl:value-of select="@Zip"/></xp-Zip>
       <Mainaddress>
          <xsl:for-each select="focus-address/addressAll/AddressLine">
          <Addressall>
             <xp-Address-Line><xsl:value-of select="focus-address/addressAll/@AddressLine"/></xp-Address-Line>
          </Addressall>
          </xsl:for-each>
       </Mainaddress>
    </Focus-Address>
    </xsl:template>
    </xsl:stylesheet>

    Cobol program:

    Data Division.
    Working-Storage Section.
    01 Focus-Address.
        02 xp-TimeStamp Pic 9(8).
        02 xp-Name Pic X(64).
        02 xp-City Pic X(32).
        02 xp-State Pic X(2).
        02 xp-Zip Pic 9(5).
        02 Mainaddress.
             20 Addressall OCCURS 1 TO 2 TIMES
                            DEPENDING ON Address-Lines.
                  30 xp-Address-Line Pic X(64).
    01 Done Pic X.
    01 Address-lines Pic 9.
    Copy "../cpy/lixmlall.cpy".

    Procedure Division.
    A. Display "Example-3 - Illustrate EXPORT FILE & IMPORT FILE"
                                        " with OCCURS DEPENDING".
         XML INITIALIZE.
         If Not XML-OK Go to Z.

         Move 2 to Address-Lines.

         Move Spaces to Focus-Address.

          XML IMPORT FILE
                Focus-Address
                "focus3"
                "Focus-Address"
                "example3.xsl".
         
          If Not XML-OK Go to Z.
          Display "focus3.xml imported by XML IMPORT FILE".
          Display xp-Name.
          Display xp-Address-Line(1).
          Display xp-Address-Line(2).
          Display xp-City xp-State xp-Zip.
          Display xp-TimeStamp.

           

    AddressLine is an attribute in the XML, but the XPATH in the stylesheet treats its as an element.   Try

    <xsl:for-each select="focus-address/addressAll">
          <Addressall>
             <xp-Address-Line><xsl:value-of select="@AddressLine"/></xp-Address-Line>
          </Addressall>
          </xsl:for-each>