xslt1.0 to convert dateTimeto YYYY/MM/DD - xslt

I am trying to convert a date received in xs:dateTime format to YYYY/MM/DD input received is : 2002-05-30T09:00:00
Output expected 2002/05/30

You could use a combination of substring-before() and translate()...
translate(substring-before(normalize-space(),'T'),'-','/')
Full example...
XML Input
<doc>2002-05-30T09:00:00</doc>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="doc">
<xsl:copy>
<xsl:value-of select="translate(substring-before(normalize-space(),'T'),'-','/')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<doc>2002/05/30</doc>
Fiddle: http://xsltfiddle.liberty-development.net/6rewNxy

Related

How to add a new element in an xml which will take the value(portion) of the existing element

I wonder how to put XSLT for converting an xml as below.
My Input xml:
<Name>
<Dept>Static-Computers</Dept>
<Name>SomeName</Name>
<DateJoined>Somevalue</DateJoined>
</Name>
I want to convert this input xml by adding a new element named 'DeptName' which copies the second portion of the string from Dept element.
My expected Output:
<Name>
<Dept>Static-Computers</Dept>
<DeptName>Computers</DeptName>
<Name>SomeName</Name>
<DateJoined>Somevalue</DateJoined>
</Name>
try this:
<?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" 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="Dept">
<xsl:copy-of select="."/>
<DeptName>
<xsl:value-of select="substring-after(., '-')"/>
</DeptName>
</xsl:template>
</xsl:stylesheet>

Filter out specific String using XSLT

Input :
<img xlink:href="figure_one"><?isoimg-id 9324-098_kr1figure1.JPG?></img>
I want to filter out as .jpg part from above input using XSLT. I have no idea to how can I filter out it.
You could simply remove the file extension (regardless of what it is) by using the substring-before() function:
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="processing-instruction('isoimg-id')">
<xsl:processing-instruction name="isoimg-id">
<xsl:value-of select="substring-before(., '.')" />
</xsl:processing-instruction>
</xsl:template>
</xsl:stylesheet>

XSLT 2.0 reformatting dates

Say I have a date that I composed in the form "2018-03-00". I want this to be "00-MAR-18". What functions, in XSLT 2.0, if any, are available for this.
I was looking for month(3) = MAR. And I was going to compose the additional fields myself.
Take a look at format-date().
With the day being "00" your date isn't castable as an xs:date. If you really have "00", you won't be able to use format-date().
If your dates are castable as xs:date's, here's an example...
XML Input
<test>2018-03-01</test>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:value-of select="format-date(., '[D01]-[MN,*-3]-[Y01]')"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<test>01-MAR-18</test>
Fiddle: http://xsltfiddle.liberty-development.net/3NzcBtj
EDIT
Since your dates aren't really dates, you could create your own function...
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:so="stackoverflow.test">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:function name="so:format_month">
<xsl:param name="date_string"/>
<xsl:variable name="months" select="(
'JAN','FEB','MAR',
'APR','MAY','JUN',
'JUL','AUG','SEP',
'OCT','NOV','DEC')"/>
<xsl:variable name="date_tokens" select="tokenize($date_string,'-')"/>
<xsl:value-of select="($date_tokens[3],
$months[number($date_tokens[2])],
substring($date_tokens[1],3,2))" separator="-"/>
</xsl:function>
<xsl:template match="/*">
<xsl:copy>
<xsl:value-of select="so:format_month(.)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Fiddle: http://xsltfiddle.liberty-development.net/3NzcBtj/2

Convert string to binary base64

Is there any way on how to convert a string to binary base64? I've seen many references but it didn't work in my end. For example I have this input file:
<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Data>
<Binary>
<RawData>This element should convert string to binary base64.</RawData>
</Binary>
</Data>
</RootElement>
I need to generate:
<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Data>
<Binary>
<RawData>VGhpcyBlbGVtZW50IHNob3VsZCBjb252ZXJ0IHN0cmluZyB0byBiaW5hcnkgYmFzZTY0Lg==</RawData>
</Binary>
</Data>
I created an xslt and used the namespace I've seen online:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dp="http://www.datapower.com/extensions">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RawData">
<xsl:element name="RawData">
<xsl:value-of select="dp:encode(., 'base-64')"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Thank you.
There is a pure XSLT 1.0 solution that works for any XSLT processor: JAXP, Saxon, Xalan, Xsltproc, Microsoft:
Download base64.xsl
Download base64_binarydatamap.xml
Use XSLT 1.0:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b64="https://github.com/ilyakharlamov/xslt_base64">
<xsl:output method="xml"/>
<xsl:include href="base64.xsl"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/RootElement/Data/Binary/RawData">
<xsl:call-template name="b64:encode">
<xsl:with-param name="asciiString" select="text()"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>

XSLT for converting empty body to a SUCCESS message

I am trying to replace an empty response body, which is getting generated by a successful DELETE operation with a "Success" message via XSLT.
Can anyone help me with the XSLT which can simply print a SUCCESS message, if the response body is empty.
Response Body will be:
<response status="204"> </response>
Expected output:
<response status="204">SUCCESS</response>
Update from comment: If the response is not empty then it should print the same what comes back as a response.
Currently using the below XSLT:
<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="*[not(*) and not(normalize-space())]">
<xsl:element name="{name()}" namespace="{namespace-uri()}"/>
</xsl:template>
</xsl:stylesheet>
But it gives:
<response/>
as output.
Can anyone help me with the XSLT which can simply print a SUCCESS
message, if the response body is empty.
If the response is not empty then it should print the same what comes
back as a response.
Then try:
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="/response[not(node())]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:text>SUCCESS</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>