Add current date into XML tag

2019-07-13 08:39发布

I'm newbie in xslt, and i have problem which i have no idea how to solve. I had to remove empty tags from my xml code. I did that and it works fine. But now i need to put ONLY into ccb:correlationId tag current date (timestamp). My xslt code:

<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="*"/>

<xsl:template match="*[descendant::text() or descendant-or-self::*/@*[string()]]">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*[string()]">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

And that's my xml example:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ccbs="http://atos.net/ccbs_ba/">
   <soapenv:Header/>
   <soapenv:Body>
      <ccbs:CCBSSaveAccountRequest>
         <ccbs:metric>
            <ccbs:system>
                <ccbs:test3></ccbs:test3>
            </ccbs:system>
            <ccbs:serviceProviderId></ccbs:serviceProviderId>
            <ccbs:correlationId>przyklad</ccbs:correlationId>
         </ccbs:metric>
         <!--Optional:-->
         <ccbs:effectiveTime>null</ccbs:effectiveTime>
         <!--Optional:-->
         <ccbs:status>status</ccbs:status>
      </ccbs:CCBSSaveAccountRequest>
   </soapenv:Body>
 </soapenv:Envelope>

Anyone could help me? Thanks.

标签: xslt
1条回答
Bombasti
2楼-- · 2019-07-13 09:20

Add the following to your stylesheet (XSLT 2.0, implemented and testable here):

<xsl:template match="ccb:correlationId" priority="5">
    <xsl:copy>
        <!-- in case you have attributes (not in your source) -->
        <xsl:apply-templates select="@*" />
        <!-- in case you need to keep current value as well -->
        <xsl:value-of select="." />
        <!-- current date/time -->
        <xsl:value-of select="current-dateTime()" />
    </xsl:copy>
</xsl:template>

Your original code shows you are using XSLT 1.0. If you cannot switch to XSLT 2.0 or 3.0, use EXSLT's date-time function:

<xsl:value-of select="date:date-time()" />

Note 1: you will need to register the following namespaces on your xsl:stylesheet root element:

  • the ccb namespace to match the same in your source document (i.e., xmlns:ccb="http://atos.net/ccbs_ba/")
  • in XSLT 1.0, the EXSLT date extension function namespace http://exslt.org/dates-and-times
  • in XSLT 2.0 well, the default function namespace is automatically set, no need to change this, unless you want to use the EXSLT extension functions.

Note 2: you didn't specify what processor you use, and not all processors support all EXSLT extension functions. Saxon, Xalan-J, libxslt and 4XSLT support it and that same link shows an MSXML implementation as well.

If you cannot use EXSLT for some reason, pass the current date/time as a parameter to your stylesheet from your calling application.

查看更多
登录 后发表回答