0
votes

I'm using XSLT 1.0 for transforming an XML(input) to another XML(output). I ran into problem where I'm not able to transform one of the element of a particular node in the XSLT.

Here is my input XML :

    <?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
     <payload>
       <order>
          <Id>12345</Id>
          <dateDelivery>2018-03-29T14:00:00 EST</dateDelivery>
          <play>
            <title>Empire Burlesque</title>
            <artist>Bob Dylan</artist>
            <country>USA</country>
            <company>Columbia</company>
            <price>10.90</price>
            <year>1985</year>
          </play>
          <play>
            <title>Esque</title>
            <artist>Bylan</artist>
            <country>CA</country>
            <company>bia</company>
            <price>16.90</price>
            <year>2018</year>
          </play>         
       </order>
     </payload>
  </cd>
 </catalog>

XSLT :

    <?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
            <SOAP-ENV:Body>
                <SongUpdate_Request>
                    <xsl:apply-templates select="//cd"/>
                </SongUpdate_Request>
            </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
    </xsl:template>
    <xsl:template match="//cd">
        <xsl:call-template name="getHeader"/>
        <xsl:call-template name="getPayload"/>
    </xsl:template>
    <xsl:template name="getHeader">
        <header>
            <header>
                <Id_T>
                    <xsl:value-of select="//payload/order/Id"/>
                </Id_T>
            </header>
        </header>
    </xsl:template>
    <xsl:template name="getPayload">
        <songEvent>
            <xsl:call-template name="getOrder"/>
        </songEvent>
    </xsl:template>
    <xsl:template name="replace">
        <xsl:param name="dd"/>
        <xsl:param name="searchString"> EST</xsl:param>
        <xsl:param name="replaceString">.000Z</xsl:param>
        <xsl:choose>
            <xsl:when test="contains($dd,$searchString)">
                <xsl:value-of select="substring-before($dd,$searchString)"/>
                <xsl:value-of select="$replaceString"/>
                <!--  recursive call -->
                <xsl:call-template name="replace">
                    <xsl:with-param name="dd" select="substring-after($dd,$searchString)"/>
                    <xsl:with-param name="searchString" select="$searchString"/>
                    <xsl:with-param name="replaceString" select="$replaceString"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$dd"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="getOrder">      
            <xsl:call-template name="replace">
                <xsl:with-param name="dd" select="//*:payload/*:order/*:dateDelivery"/>
            </xsl:call-template>
        <xsl:variable name="OrderPresent">
            <xsl:value-of select=".//*:order"/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="$OrderPresent!=''">
                <xsl:copy-of select=".//*:order"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Output XML :

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SongUpdate_Request>
         <header>
            <header>
               <Id_T>12345</Id_T>
            </header>
         </header>
         <songEvent>2018-03-29T14:00:00.000Z
                <order>
                  <Id>12345</Id>
                  <dateDelivery>2018-03-29T14:00:00 EST</dateDelivery>
                     <play>
                           <title>Empire Burlesque</title>
                           <artist>Bob Dylan</artist>
                           <country>USA</country>
                           <company>Columbia</company>
                           <price>10.90</price>
                           <year>1985</year>
                     </play>
                     <play>
                           <title>Esque</title>
                           <artist>Bylan</artist>
                           <country>CA</country>
                           <company>bia</company>
                           <price>16.90</price>
                           <year>2018</year>
                     </play>          
               </order>
         </songEvent>
      </SongUpdate_Request>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

So if you observer, I'm trying to change the date format from EST to "Z". But I'm not sure how to apply while transforming in the XSLT. Meaning I want to see the output xml like :

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
      <SongUpdate_Request>
         <header>
            <header>
               <Id_T>12345</Id_T>
            </header>
         </header>
         <songEvent>
                <order>
                  <Id>12345</Id>
                  *<dateDelivery>2018-03-29T14:00:00.000Z</dateDelivery>*
                     <play>
                           <title>Empire Burlesque</title>
                           <artist>Bob Dylan</artist>
                           <country>USA</country>
                           <company>Columbia</company>
                           <price>10.90</price>
                           <year>1985</year>
                     </play>
                     <play>
                           <title>Esque</title>
                           <artist>Bylan</artist>
                           <country>CA</country>
                           <company>bia</company>
                           <price>16.90</price>
                           <year>2018</year>
                     </play>          
               </order>
         </songEvent>
      </SongUpdate_Request>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Any suggestions or help is greatly appreciated. Thanks!!

1
Are you aware that EST is 5 hours behind UTC, so that 2018-03-29T14:00:00 EST is actually 2018-03-29T19:00:00 Z?michael.hor257k
@michael.hor257k Thanks for the reply. Yes I'm aware. But I still wanted to do the transformation as the end system is expecting the date format as 2018-03-29T14:00:00.000Zkuti
I understand the change in format, but what you're asking to do is a change of the actual value. It's like changing $100 to €100 without converting the amount using the exchange rate.michael.hor257k
yes I've to change the actual value.kuti
Could you please help me on how to change the value?kuti

1 Answers

1
votes

If you want to modify an element, you cannot copy it. You need to have a template matching it, where you can specify what to output instead of the original contents. In your example, this could be simply:

<xsl:template match="dateDelivery">
    <xsl:copy>
        <xsl:value-of select="substring(., 1, 19)"/>
        <xsl:text>.000Z</xsl:text>
    </xsl:copy>
</xsl:template>

Here's a complete stylesheet that produces the XML shown in your question:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/catalog">
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
        <SOAP-ENV:Body>
            <SongUpdate_Request>
                <xsl:apply-templates select="cd/payload"/>
            </SongUpdate_Request>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
</xsl:template>

<xsl:template match="payload">
    <header>
        <header>
            <Id_T>
                <xsl:value-of select="order/Id"/>
            </Id_T>
        </header>
    </header>
    <songEvent>
        <xsl:apply-templates/>
    </songEvent>    
</xsl:template>

<xsl:template match="dateDelivery">
    <xsl:copy>
        <xsl:value-of select="substring(., 1, 19)"/>
        <xsl:text>.000Z</xsl:text>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Note:
As mentioned in the comments to your question, this modifies the actual value of the dateDelivery element by making it 5 hours earlier than the original time.