Convert dates from yyyy-mm-dd to dd / mm / yyyy, for which zero or empty dates in xslt

I wanted to change the date order from yyyy-mm-dd to dd / mm / yyyy, but use null or empty dates. I used code

<!--         Data Submitted -->
<xsl:template match="data_submitted">
<data_submitted>
    <xsl:if test="data_submitted != ''">
            <xsl:value-of select=
           "concat(substring(.,9), '/',
                  (substring(.,6,2)), '/',
                 substring(.,1,4))"
           />
    </xsl:if>
</data_submitted>   
</xsl:template>
<!--                                           -->

but these are dated dates in general. Leaving the given in null dates appearing as // in my result. I'm sure I probably got it, if in the wrong place or something like that, but I want the tag to appear even if it's empty.

Any suggestions?

Update:

, xml, null empty, , , . xml: data_submitted> </data_submitted> 8 . test="data_submitted != ' '" , : test=".!= ''", 8 .

+3
4

"" . () , :

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

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

    <xsl:template match="data_submitted">
        <data_submitted>
            <xsl:variable name="year" select="substring-before(.,'-')"/>
            <xsl:variable name="month" select="substring-before(substring-after(.,'-'),'-')"/>
            <xsl:variable name="day" select="substring-after(substring-after(.,'-'),'-')"/>

            <xsl:if test=".!=''">
                <xsl:value-of select=
                    "concat($day,'/',
                    $month,'/',
                    $year)"
                    />
            </xsl:if>
        </data_submitted>   
    </xsl:template>

</xsl:stylesheet>

:

<data>
    <data_submitted>
        yyyy-mm-dd
    </data_submitted>
    <data_submitted/>
</data>

:

<data>
   <data_submitted>dd/mm/yyyy</data_submitted>
   <data_submitted/>
</data>

?

data_submitted, ., .

text() :

test="boolean(./text())"

( )

test="boolean(text())"

false, 0.

+1

, " ". , data_submitted , , , data_submitted, . , , data_submitted != '' not(data_submitted = ''). , , "! =" XSLT, , .

+1

, - data_submitted (data_submitted) node . , data_submitted XML- data_submitted.

, / :

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

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

    <xsl:template match="data_submitted/text()">
        <xsl:value-of select=
        "concat(substring(.,9),
                 '/',
                 substring(.,6,2),
                 '/',
                 substring(.,1,4)
             )
  "/>
    </xsl:template>
</xsl:stylesheet>

, XML-:

<t>
  <a/>
  <b>
     <data_submitted>2011-06-07</data_submitted>
     <data_submitted/>
    </b>
    <data_submitted>1912-05-12</data_submitted>
    <c/>
</t>

, :

<t>
   <a/>
   <b>
      <data_submitted>07/06/2011</data_submitted>
      <data_submitted/>
   </b>
   <data_submitted>12/05/1912</data_submitted>
   <c/>
</t>

  • node "as-is"

  • - node data_submitted. . , data_submitted, " " .

. - XSLT.

XSLT .

+1

Typically, when testing, if node exists, you put the xpath expression in your "test" statement. Without seeing your xml, I would suggest that you can simply remove the "! =", And everything will be fine.

0
source

All Articles