2

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.

Abel
  • 56,041
  • 24
  • 146
  • 247
andreww
  • 223
  • 6
  • 16

1 Answers1

2

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.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • Hey, i got error message "make sure if your xsl is valid (...). I can upgrade into 2.0 version. But while trying to use code for 2.0 version i still got an error said before.. – andreww Sep 08 '15 at 11:55
  • I got an error that ccb namespace is undeclared. How do do that? – andreww Sep 08 '15 at 12:01
  • @maciek2791: that is my first comment in my first note. You need to add it to the `xsl:stylesheet` element same way you did with the exslt namespace and other namespaces. And I still wonder what processor you are using... – Abel Sep 08 '15 at 12:02
  • Hey, Abel I Edit my first code. Please look on that. – andreww Sep 08 '15 at 12:12
  • @maciek2791 in addition, I had a typo, `ID` >> `Id` and the default priority was lower than the predicated match. Both fixed above. I can't repro your whitespace, the result shows XML, see: http://xsltransform.net/pPqsHUu/1 (includes your code) – Abel Sep 08 '15 at 12:22
  • It's working... thanks everyone for your help and for you patience... Regards :) – andreww Sep 08 '15 at 12:38
  • I have one more question :) Is there a data function which display only date + HH+MM+SS+MS eg. 2015-09-09 14:47:00:456 - this format. How i can accept your answer?:) – andreww Sep 08 '15 at 12:48
  • @maciek2791, it may be helpful to take the [tour], as SO is not the same as other forums. But _just click the grey checkmark_, top-left of the answer. For formatting dates, now that you use XSLT 2.0, use [`format-date` or `format-dateTime`](http://www.sixtree.com.au/articles/2013/formatting-dates-and-times-using-xslt-2.0-and-xpath/). – Abel Sep 08 '15 at 12:52
  • Ok thanks. Is there any function which will generate random number? I would like to add into my date some random number. Date concat number. Is this possible? How it would looks like? "Thanks for the feedback! Once you earn a total of 15 reputation, your votes will change the publicly displayed post score" so i have wait :) – andreww Sep 08 '15 at 13:09
  • Ok,I did that :) Abel, while trying to concat number and date i got an error... Could you last time take a look what i did? http://xsltransform.net/pPqsHUu/2 – andreww Sep 08 '15 at 13:25
  • @maciek2791, thanks :) (I will remove my OT comments). you are trying to add a Xalan random number function to a Saxon processor. TThey are not compatible, Saxon does not know of Xalan's extension functions. Search xslt + random number and you find enough hints how to fix this. Be aware that random numbers are considered a side-effect in a functional language such as XSLT and XPath. Consider asking a new question though, this information gets too hidden in the thread.... – Abel Sep 08 '15 at 13:29