Using XSLT to transform date format from yyyy-mm-dd to dd-mm-yy - xslt-1.0

I am reading an XML document in which there is a date tag as shown below.
<uabDate>2014-07-23</uabDate>
Now I am reading this XML from XSL and trying to parse it, but I want to translate that date value into a format like 23-07-14.
What I have tried is given below.
Here I am calling the template that will store uabDate in a variable businessDate and then calling the template
<xsl:call-template name="convertDateToDDMMYYYY_template">
<xsl:with-param name="b1"
select="$s2Date/uabDate"/>
</xsl:call-template>
and here is the definition of the named template:
<xsl:template name="convertDateToDDMMYYYY_template">
<xsl:param name="b1"/>
<xsl:variable name="DateVar">
<xsl:value-of select="concat(
substring(./b1,1,4),
'-',
substring(./b1,6,2),
'-',
substring(./b1,9,2))"/>
</xsl:variable>
</xsl:template>

You wrote your template wrong. First of all you are putting the result into a variable, called DateVar, and don't output it.
Secondly you got a parameter called b1 and parameters should be called using $b1.
And third issue is you are formatting the date same as the input. Instead of changing the date format.
So for example if I use this input XML:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<uabDate>2014-07-23</uabDate>
</data>
And this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="uabDate">
<xsl:copy>
<xsl:call-template name="convertDateToDDMMYYYY_template">
<xsl:with-param name="b1" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="convertDateToDDMMYYYY_template">
<xsl:param name="b1"/>
<xsl:value-of select="concat(substring($b1,9,2),'-',substring($b1,6,2),'-',substring($b1,1,4))"/>
</xsl:template>
</xsl:stylesheet>
It creates this output:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<uabDate>23-07-2014</uabDate>
</data>

Related

XSLT 1.0 multiple call of a template

I need to replace strings in an XML file. I'm trying to use XSLT to do it. I want to use call-template for each string I need to replace.
When I make multiple call to the template, only the last call works fine.
The XML file I need to change : I want to replace the strings
‘
and
—
by spaces
<?xml version="1.0" encoding="UTF-8"?>
<RAPPORT>
<reason>start test_145 : ‘ and test_ 151 : — _end_test</reason>
</RAPPORT>
The template I use :
<xsl:template name="globalReplace">
<xsl:param name="outputString"/>
<xsl:param name="target"/>
<xsl:param name="replacement"/>
<xsl:choose>
<xsl:when test="contains($outputString,$target)">
<xsl:value-of select="concat(substring-before($outputString,$target),$replacement)"/>
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="substring-after($outputString,$target)"/>
<xsl:with-param name="target" select="$target"/>
<xsl:with-param name="replacement" select="$replacement"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$outputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The multiple calls :
<xsl:template match="text()">
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="."/>
<xsl:with-param name="target" select="'‘'"/>
<xsl:with-param name="replacement" select="' '"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="."/>
<xsl:with-param name="target" select="'—'"/>
<xsl:with-param name="replacement" select="' '"/>
</xsl:call-template>
</xsl:template>
Expected result :
<?xml version="1.0" encoding="UTF-8"?>
<RAPPORT>
<reason>debut test_test_145 et test 151 _fin test</reason>
</RAPPORT>
What I get in fact :
<?xml version="1.0" encoding="UTF-8"?>
<RAPPORT>
<reason>debut test_test_145**PUI** et test 151 _fin test</reason>
</RAPPORT>
PUI means an unexpected character instead of the needed space
You have two templates that match text(). Only the last of these is applied - see: https://www.w3.org/TR/1999/REC-xslt-19991116#conflict
In the given example, you could simply use the translate() function to do all the work at once, since the "strings" you want to replace are actually characters:
XML
<?xml version="1.0" encoding="UTF-8"?>
<RAPPORT>
<reason>start test_145 : ‘ and test_ 151 : — _end_test</reason>
</RAPPORT>
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="text()">
<xsl:value-of select="translate(., '‘—', ' ')"/>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<RAPPORT>
<reason>start test_145 : and test_ 151 : _end_test</reason>
</RAPPORT>
Many thanks !
It works fine this way !
I improved it inserting my result into a variable :
<xsl:template match="text()">
<xsl:variable name="premierePasse">
<xsl:value-of select="translate(., '‘—’',' ')"/>
</xsl:variable>
<xsl:value-of select="translate($premierePasse, 'œ', 'œ')"/>
</xsl:template>

xslt - create empty file using xslt 1.0

I am trying to create an empty file through xslt.
The input sample is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Businessman>
<siblings>
<sibling>John </sibling>
</siblings>
<child> Pete </child>
<child> Ken </child>
</Businessman>
When the input contains any presence of 'child' tags, it should produce the file AS IS. When the input does not have any 'child' tag, I need an empty file (0 byte file) created.
This is what I tried:
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="#*|node()">
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This gives the file unchanged when there is any 'child' tag present. But did not produce any empty file when there is no 'child' tag.
The file I need to test will look like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Businessman>
<siblings>
<sibling>John </sibling>
</siblings>
</Businessman>
Any help would be great!
Thanks
If you want the processor to go to the trouble of opening the output file, you have to give it something to write to the output file. Try an empty text node. And you only need to make the decision 'copy or not?' once.
One way to make the decision just once and produce empty output if the condition is not met would be to replace your template with:
<xsl:template match="/">
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:copy-of select="*"/>
</xsl:when>
<xsl:otherwise>
<xsl:text/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This works as expected with xsltproc. (If you find yourself getting a file containing an XML declaration and nothing else, try adjusting the parameters on xsl:output.)
But when I have found myself with a similar situation (perform this transform if condition C holds, otherwise ...), I have simply added a template for the document node that would look something like this for your case:
<xsl:choose>
<xsl:when test="/Businessman/child">
<xsl:apply-templates/>
</
<xsl:otherwise>
<xsl:message terminate="yes">No children in this input, dying ...</
</
</
That way I get no output at all rather than zero-length output.
Simple enough - Just don't try to do everything in one template, don't forget to omit the xml declaration and get the xpath right:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="Businessman[child]" priority="9">
<xsl:element name="Businessman">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="Businessman" priority="0" />
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

How to Parse Query parameters in XSLT

I have a requirement I will get the URL in the format like this below
https://sample.com?first=one&second=two&third=three
How can I form an xml structure below which will be formed by making use of this query parameters
<first>one</first><second>two</second><third>three</third>
can Please somebody help me with thisand provide me an xslt for this requirement
Given the following XML input (note the escaping of the ampersand character):
<URL>https://sample.com?first=one&second=two&third=three</URL>
the folowing stylesheet:
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:template match="/">
<output>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after(URL, '?')"/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'&'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<xsl:element name="{substring-before($token, '=')}">
<xsl:value-of select="substring-after($token, '=')"/>
</xsl:element>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
will return:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<first>one</first>
<second>two</second>
<third>three</third>
</output>
Note that this will work only if the query field names are also valid XML element names.

Passing a parameter does not seem to work

I need a parameter to be send to a template to help with processing the right nodes in an external second xml file (this to keep the amount of templates small).
I've found that the following code send the parameter and processes it correctly:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
This is text1
<xsl:apply-templates mode="my-mode">
<xsl:with-param name="testParam" select="'TEST_PARAMETER'"/>
</xsl:apply-templates>
This is text2
</xsl:template>
<xsl:template match="DialStats" mode="my-mode">
<xsl:param name="testParam" />
<br></br>
This is text 3<br></br>
<xsl:value-of select="$testParam" /><br></br>
This is text 4<br></br>
</xsl:template>
</xsl:stylesheet>
When I implement the second file I've found that the following no longer passes the parameter:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
This is text1
<xsl:apply-templates select="document('file://D:/DATA/Marquee/DATA/MarqueeStats.xml')" mode="my-mode">
<xsl:with-param name="testParam" select="'TEST_PARAMETER'"/>
</xsl:apply-templates>
This is text2
</xsl:template>
<xsl:template match="DialStats" mode="my-mode">
<xsl:param name="testParam" />
<br></br>
This is text 3<br></br>
<xsl:value-of select="$testParam" /><br></br>
This is text 4<br></br>
</xsl:template>
It calls the template correctly, as I can see both the lines with 'This is text x". So the parameter is gone. I did find some articles suggesting the match= needs to be set, so I matched that with the xml root:
<DialStats>
<Countries Country="Denmark">
<Products Product="MR">
. . .
Any suggestions on how to resolve this?
BTW this is just the basics I need to get right as a lot more code need to be added.

XSL 1.0 - Hiding Child nodes when child element is Null

I have been trying to make child elements of my structure hide based upon them being empty.
From reading other posts I found this Remove parent node if a child node is empty but I don't understand it enough to implement it in my XSL. I have had a try at applying the linked post to my XSL but it does not make the desired changes to my output.
So my XML is like this:
<?xml version="1.0" encoding="UTF-8" ?>
<exchange>
<sce>
<sce.srs>
<sce_scjc.sce.srs>140008305/1</sce_scjc.sce.srs>
<sce_seq2.sce.srs>01</sce_seq2.sce.srs>
<sce_stuc.sce.srs>140008305</sce_stuc.sce.srs>
<spr>
<spr.cams>
<spr_code.spr.cams>140008305/1</spr_code.spr.cams>
<prs_code.spr.cams>77711925</prs_code.spr.cams>
<prs>
<prs.mensys>
<prs_code.prs.mensys>77711925</prs_code.prs.mensys>
<prs_name.prs.mensys>Johan</prs_name.prs.mensys>
</prs.mensys>
</prs>
</spr.cams>
</spr>
</sce.srs>
<sce.srs>
<sce_scjc.sce.srs>151516736/1</sce_scjc.sce.srs>
<sce_seq2.sce.srs>01</sce_seq2.sce.srs>
<sce_stuc.sce.srs>151516736</sce_stuc.sce.srs>
<spr>
<spr.cams>
<spr_code.spr.cams>151516736/1</spr_code.spr.cams>
<prs_code.spr.cams>77709062</prs_code.spr.cams>
<prs>
<prs.mensys>
<prs_code.prs.mensys>77709062</prs_code.prs.mensys>
<prs_name.prs.mensys>Evangelia</prs_name.prs.mensys>
</prs.mensys>
</prs>
</spr.cams>
</spr>
</sce.srs>
<sce.srs>
<sce_scjc.sce.srs>150052468/1</sce_scjc.sce.srs>
<sce_seq2.sce.srs>01</sce_seq2.sce.srs>
<sce_stuc.sce.srs>150052468</sce_stuc.sce.srs>
<spr>
<spr.cams>
<spr_code.spr.cams>150052468/1</spr_code.spr.cams>
<prs_code.spr.cams/>
</spr.cams>
</spr>
</sce.srs>
</sce>
</exchange>
And my XSL looks like the passaage below. I have a nil element template that I added in as I thought looking for a value was easier than looking for a nulll so happy for it to come out if not needed.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" exclude-result-prefixes="xd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/exchange/sce">
<ImportTask>
<EntityRelationshipEntities>
<xsl:apply-templates select="node()|#*"/>
</EntityRelationshipEntities>
</ImportTask>
</xsl:template>
<xsl:template name="nilElement">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="string($value)">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="xsi:nil" namespace="http://www.w3.org/2001/XMLSchema-instance">True</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="sce.srs[not(*/AttributeValue[not(#AttributeValue='True')])]">
<xsl:for-each select="spr/spr.cams">
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode><xsl:value-of select="../../sce_stuc.sce.srs"/></EntityCode>
<AttributeValue>
<xsl:call-template name="nilElement">
<xsl:with-param name="value" select="prs/prs.mensys/prs_name.prs.mensys"/>
</xsl:call-template>
</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</xsl:for-each>
</xsl:template>
<xsl:template match="AttributeValue[#AttributeValue = 'True']"/>
</xsl:stylesheet>
So this currently gives me:
<ImportTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EntityRelationshipEntities>
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>151514490</EntityCode>
<AttributeValue xsi:nil="True"/>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>140008305</EntityCode>
<AttributeValue>Johan</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
<EntityRelationshipEntity>
<EntityCode>151516736</EntityCode>
<AttributeValue>Evangelia</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</EntityRelationshipEntities>
</ImportTask>
What I would like to produce is out those where is not null. This would mean that the child below is not output:
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>151514490</EntityCode>
<AttributeValue xsi:nil="True"/>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
But the others would be output something like this:
<ImportTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EntityRelationshipEntities>
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>140008305</EntityCode>
<AttributeValue>Johan</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
<EntityRelationshipEntity>
<EntityCode>151516736</EntityCode>
<AttributeValue>Evangelia</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</EntityRelationshipEntities>
</ImportTask>
Can someone help me apply this correctly?
Many thanks
Jonah
Perhaps I am missing something, but couldn't this be simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/exchange/sce">
<ImportTask xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EntityRelationshipEntities>
<xsl:apply-templates select="sce.srs/spr/spr.cams[string(prs/prs.mensys/prs_name.prs.mensys)]"/>
</EntityRelationshipEntities>
</ImportTask>
</xsl:template>
<xsl:template match="spr.cams">
<EntityRelationshipEntity>
<ErRef>ERREF_21</ErRef>
<EntityCode>
<xsl:value-of select="../../sce_stuc.sce.srs"/>
</EntityCode>
<AttributeValue>
<xsl:value-of select="prs/prs.mensys/prs_name.prs.mensys"/>
</AttributeValue>
<Action>VALUEONLY</Action>
</EntityRelationshipEntity>
</xsl:template>
</xsl:stylesheet>