Replacing 3 characters by 1 in a string with XSLT 1.0 - replace

I'm working on a xsl template and one specific string is giving me a hard time.
<prixmenu>29.00-90.00</prixmenu>
Desired output :
<prixmenu>29€-90€</prixmenu>
The whole code I have is :
XML (input) :
<?xml version="1.0" encoding="UTF-8"?>
<data>
<etablissement not="15.0" etoile="2" >
<nom>L'Auberge Asterix</nom>
<index>AUBERGE ASTERIX (L')</index>
<id>0123456789</id>
<regionindex>Paris</regionindex>
<equipe>
<chef>Ratatouille</chef>
</equipe>
<pictoPratique cave_remarquable="0" coeur="0" ></pictoPratique>
<coordonnees>
<adresse>Random adress</adresse>
<tel>01 23 45 67 89/tel>
<fermetures>mam,maa,</fermetures>
<pratique terrasse="0"
voiturier="0"
parcPrive="1"
handicap="0"
airConditione="0"
piscine="0"
tennis="0"
chien="1"
relaischat="0"
delivery="0"
clickAndCollect="0"
itineraireGourmand="0"
menu_kids="1"
hebergement="0"
></pratique>
</coordonnees>
<texte>Lorem ipsum</texte>
<prixrestau>
<prixcarte>0</prixcarte>
<prixmenu>29.00-90.00</prixmenu>
</prixrestau>
</etablissement>
</data>
XSL stylesheet :
<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="data | node()[not(self::coordonnees)]">
<xsl:copy>
<xsl:apply-templates select="nom" />
<xsl:choose>
<xsl:when test="#etoile = 1">
<etoile>1 étoile</etoile>
</xsl:when>
<xsl:when test="#etoile > 1">
<etoile><xsl:value-of select="#etoile" /> étoiles</etoile>
</xsl:when>
</xsl:choose>
<xsl:apply-templates select="index" />
<xsl:apply-templates select="id" />
<xsl:apply-templates
select="#*[not(.='0')][name( ) != 'etoile'] | node()[not(self::index)][not(self::id)][not(self::prixrestau)][not(self::nom)][not(self::etoile)]" />
</xsl:copy>
</xsl:template>
<xsl:template match="#*[name( ) != 'etoile']">
<xsl:element name="{name()}">
<xsl:value-of select="number(.)" />
</xsl:element>
</xsl:template>
<xsl:template match="prixrestau/prixmenu" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="$pText">
<xsl:value-of select="number(substring-before(concat($pText, '-'), '-'))"/>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, '-')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="coordonnees">
<xsl:copy>
<xsl:apply-templates select="adresse" />
<xsl:apply-templates select="tel" />
<xsl:element name="prixrestau">
<xsl:value-of select="translate(../prixrestau/prixmenu,'.00.','€')"/>
<xsl:apply-templates select="../prixrestau/prixcarte[not(.='0')]" />
<xsl:element name="prixmenu">
<xsl:apply-templates select="../prixrestau/prixmenu" />
</xsl:element>
</xsl:element>
<xsl:apply-templates select="fermetures" />
<xsl:apply-templates
select="#*[not(.='0')] | node()[not(self::adresse)][not(self::tel)][not(self::fermetures)]" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Desired output :
<?xml version="1.0"?>
<data>
<etablissement>
<nom>L'Auberge Asterix</nom>
<etoile>2 étoiles</etoile>
<index>AUBERGE ASTERIX (L')</index>
<id>0123456789</id>
<not>13</not>
<regionindex>Paris</regionindex>
<equipe>
<chef>Ratatouille</chef>
</equipe>
<pictoPratique/>
<coordonnees>
<adresse>Random adress</adresse>
<tel>01 23 45 67 89</tel>
<prixrestau><prixmenu>29€-90€</prixmenu></prixrestau>
<fermetures>mam,maa,</fermetures>
<pratique>
<parcPrive>1</parcPrive>
<chien>1</chien>
<menu_kids>1</menu_kids>
</pratique>
</coordonnees>
<texte>Lorem ipsum</texte>
</etablissement>
</data>
Any suggestions on how to proceed ? Should I import FXSL or switch to XSLT 2.0 as it probably be less of a pain to get to the desired result ?
I've tried splitting it with the following code (I belive it's called a 'recursive template" :
<xsl:template match="prixrestau/prixmenu" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="$pText">
<xsl:value-of select="number(substring-before(concat($pText, '-'), '-'))"/>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, '-')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
This outputs
<prixmenu>2990</prixmenu>
And i've also tried
<xsl:value-of select="translate(../prixrestau/prixmenu,'.00.','€')"/>
and the output is :
29€-9€
Because '.00.' and '€'don't have the same length (MDN reference)...

How about:
<xsl:template match="prixmenu">
<xsl:copy>
<xsl:value-of select="format-number(substring-before(., '-'), '0€')"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="format-number(substring-after(., '-'), '0€')"/>
</xsl:copy>
</xsl:template>
If you like, you could change the first xsl:value-of instruction to:
<xsl:value-of select="format-number(substring-before(., '-'), '0€-')"/>
and get rid of the xsl:text part - but I think it's better to keep it to make the code more readable.

Related

XSLT Replace Empty Node with Dummy

My requirement is as below
1) Replace Space with %20
2) Replace / with %2F
3) empty elements with value as dummy
Sample Input:
<?xml version="1.0" encoding="UTF-8" ?>
<process xmlns="http://xmlns.oracle.com/CCS/Project5/BPELProcess1">
<Sender>Sender 1</Sender>
<TransactionId>TransactionId/2</TransactionId>
<TransactionType>TransactionType5</TransactionType>
<Status>Status6</Status>
<Limit>70.73</Limit>
<Remarks>Remarks8</Remarks>
<Result>GlobalResult9</Result>
<Type>DecisionType10</Type>
<DecidedBy>DecidedBy11</DecidedBy>
<AddRequest1/>
<AddRequest2>RAMA</AddRequest2>
</process>
Required Output:
<process xmlns="http://xmlns.oracle.com/CCS/Project5/BPELProcess1">
<Sender>Sender%201</Sender>
<TransactionId>TransactionId%2F2</TransactionId>
<TransactionType>TransactionType5</TransactionType>
<Status>Status6</Status>
<Limit>70.73</Limit>
<Remarks>Remarks8</Remarks>
<Result>GlobalResult9</Result>
<Type>DecisionType10</Type>
<DecidedBy>DecidedBy11</DecidedBy>
<AddRequest1>DUMMY</AddRequest1>
<AddRequest2>RAMA</AddRequest2>
</process>
I have tried below XSLT but could not find a wat replace empty node to Dummy
eg
<AddRequest1></AddRequest1> is not coverted to <AddRequest1>DUMMY</AddRequest1>
XSLT tried is as below
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//text()">
<xsl:call-template name="rep_SPLChar">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="rep_SPLChar">
<xsl:param name="text"/>
<xsl:variable name="temp_space" select="'%20'"/>
<xsl:variable name="temp_backslash" select="'%2F'"/>
<xsl:if test="normalize-space($text) != ''">
<xsl:choose>
<xsl:when test="contains($text,' ')">
<xsl:call-template name="rep_SPLChar">
<xsl:with-param name="text"
select="concat((concat(substring-before($text,' '),$temp_space)),substring-after($text,' '))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="contains($text,'/')">
<xsl:call-template name="rep_SPLChar">
<xsl:with-param name="text"
select="concat((concat(substring- before($text,'/'),$temp_backslash)),substring-after($text,'/'))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Try adding this template to your XSLT to match "empty" elements
<xsl:template match="*[not(*)][not(normalize-space())]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:text>DUMMY</xsl:text>
</xsl:copy>
</xsl:template>

Need to perform two transformation for the same content in xsl

I have two different transformation.After first transformation get over second need to be applied but not able to do.I have referred other stackoverflow post but its not working for me.
Sample Input:
<Para>1<AAA>2<BBB>3</BBB>4</AAA>5</Para>
Requirement is like BBB may available outside of AAA as well.We need to remove all AAA and put the value in comma separated format.
Expected output after First:
<Para>12<BBB>3</BBB>45</Para>
Expected output on the Second:
<Para>12,3,45</Para>
First:
Here i am just removing tag AAA from the content.And retriving its content and child.
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match ="*/AAA">
<xsl:apply-templates/>
</xsl:template>
Second:
Here I am just appending comma between the node element by removing all the tags inside para and retrieving its content.
<xsl:param name="Para"></xsl:param>
<xsl:template match="Para">
<xsl:copy>
<xsl:variable name="BBB-element-exists">
<xsl:for-each select="node()[self::BBB ][normalize-space(.)!='']">
<xsl:if test="(self::Change and (child::BBB)) or (self::BBB)">
<xsl:value-of select="'BBBexists'"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:if test="$BBB-element-exists = 'BBBexists'">
<xsl:for-each select=".//text()">
<xsl:if test="position() >1 and not(parent::Change) ">
<xsl:value-of select="','" />
</xsl:if>
<xsl:value-of select="normalize-space()" />
</xsl:for-each>
</xsl:if>
<xsl:if test="$BBB-element-exists != 'BBBexists'">
<xsl:for-each select=".//text()">
<xsl:value-of select="normalize-space()" />
</xsl:for-each>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You will need to use at least one mode to separate processing of the second step from the first:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="#*|node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="#*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/AAA">
<xsl:apply-templates/>
</xsl:template>
<xsl:variable name="first-pass">
<xsl:apply-templates select="node()"/>
</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="$first-pass/node()" mode="step2"/>
</xsl:template>
<xsl:template match="Para" mode="step2">
<xsl:copy>
<xsl:variable name="BBB-element-exists">
<xsl:for-each select="node()[self::BBB ][normalize-space(.)!='']">
<xsl:if test="(self::Change and (child::BBB)) or (self::BBB)">
<xsl:value-of select="'BBBexists'"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:if test="$BBB-element-exists = 'BBBexists'">
<xsl:for-each select=".//text()">
<xsl:if test="position() >1 and not(parent::Change) ">
<xsl:value-of select="','" />
</xsl:if>
<xsl:value-of select="normalize-space()" />
</xsl:for-each>
</xsl:if>
<xsl:if test="$BBB-element-exists != 'BBBexists'">
<xsl:for-each select=".//text()">
<xsl:value-of select="normalize-space()" />
</xsl:for-each>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:transform>

XSLT Transform to XML

I have some xml in the following format:
<top>
<topValue Value="1#1#5" />
<topValue Value="2#2#10" />
<topValue Value="1#1#3" />
<topValue Value="2#2#30" />
</top>
and output should look like that:
<boo>
<booEnrty>
<v>5</v>
<v>10</v>
</booEnrty>
<booEnrty>
<v>3</v>
<v>30</v>
</booEnrty>
</boo>
my XSLT to transform
<boo>
<xsl:for-each select="top/topValue">
<xsl:if test="position() mod 2 = 0">
<booEnrty>
<v><xsl:value-of select="substring-after(substring-after(#Value,'#'),'#')"/></v>
</booEnrty>
</xsl:if>
</xsl:for-each>
</boo>
What should the XSLT document look like to do this transform?
Any ideas?
Thanks
Maybe someone got a better approach to this, but the XSLT below works for your case.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="top">
<boo>
<xsl:apply-templates select="topValue[position() mod 2 = 1]"/>
</boo>
</xsl:template>
<xsl:template match="topValue[position() mod 2 = 1]">
<booEntry>
<v>
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string" select="#Value" />
<xsl:with-param name="delimiter" select="'#'" />
</xsl:call-template>
</v>
<xsl:apply-templates select="following-sibling::*[1]"/>
</booEntry>
</xsl:template>
<xsl:template match="topValue">
<v>
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string" select="#Value" />
<xsl:with-param name="delimiter" select="'#'" />
</xsl:call-template>
</v>
</xsl:template>
<xsl:template name="substring-after-last">
<xsl:param name="string" />
<xsl:param name="delimiter" />
<xsl:choose>
<xsl:when test="contains($string, $delimiter)">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string"
select="substring-after($string, $delimiter)" />
<xsl:with-param name="delimiter" select="$delimiter" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$string"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
How about something short and simple?
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="/top">
<boo>
<xsl:for-each select="topValue[position() mod 2 = 1]">
<booEnrty>
<xsl:for-each select=". | following-sibling::topValue[1]">
<v>
<xsl:value-of select="substring-after(substring-after(#Value,'#'),'#')"/>
</v>
</xsl:for-each>
</booEnrty>
</xsl:for-each>
</boo>
</xsl:template>
</xsl:stylesheet>

print xpath and value of element and attribute using XSLT

I would like to print path of element and attributes if any along with values using XSLT. e.g
XML :
<root>
<node attr='abc' module='try'>
<subnode>Roshan</subnode>
<subnode>Chetan</subnode>
<subnode>Jennifer</subnode>
</node>
</root>
Output :
/root##
/root/node##
/root/node/#attr##abc
/root/node/#module##try
/root/node/subnode[1]##Roshan
/root/node/subnode[2]##Chetan
/root/node/subnode[3]##Jennifer
I am trying with below snippet, but could only print path of element and it's value
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',local-name())" />
<xsl:if
test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
<xsl:value-of
select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')" />
</xsl:if>
<!-- <xsl:call-template name="attrData"></xsl:call-template> -->
</xsl:for-each>
<xsl:text>##</xsl:text>
<xsl:apply-templates select="node()" />
</xsl:template>
I am new to XSLT. Please help!!!!
I made the following XSLT and added also the [position] to the output. You can remove that if you need.
This gives this output:
/root[1]
/root[1]/node[1]
/root[1]/node[1]/#attr[1]##abc
/root[1]/node[1]/#module[1]##try
/root[1]/node[1]/subnode[1]##Roshan
/root[1]/node[1]/subnode[2]##Chetan
/root[1]/node[1]/subnode[3]##Jennifer
With this XSLT. With the two output template you can choose how to print the Xpath.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" version="2.0">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:call-template name="generateXPath">
<xsl:with-param name="previous" select="''"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="generateXPath">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:if test="not(empty(.))">
<xsl:variable name="thisXPath" select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:variable>
<xsl:apply-templates select="." mode="output">
<xsl:with-param name="previous" select="$previous"/>
</xsl:apply-templates>
<xsl:text>
</xsl:text>
<xsl:for-each select="*|#*">
<xsl:call-template name="generateXPath">
<xsl:with-param name="previous" select="$thisXPath"/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="output">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:variable name="thisXPath">
<xsl:value-of select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:value-of>
<xsl:if test="not(*)">
<xsl:value-of select="concat('##',text())"></xsl:value-of>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$thisXPath" />
</xsl:template>
<xsl:template match="#*" mode="output">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:variable name="thisXPath" select="concat($previous, '/#', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']','##',.)"></xsl:variable>
<xsl:value-of select="$thisXPath" />
</xsl:template>
</xsl:stylesheet>

Simple XSLT template

There is XML document:
<data>how;many;i;can;tell;you</data>
Need to get XML using XSLT version 1:
<manydata>
<onedata>how</onedata>
<onedata>many</onedata>
<onedata>i</onedata>
<onedata>can</onedata>
<onedata>tell</onedata>
<onedata>you</onedata>
</manydata>
How I can do it?
UPDATE:
Output format must be XML.
This recursive solution is probably one of the shortest possible:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="data">
<manydata><xsl:apply-templates/></manydata>
</xsl:template>
<xsl:template match="text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<onedata>
<xsl:value-of select=
"substring-before(concat($pText,';'),';')"/>
</onedata>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText,';')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document;
<data>how;many;i;can;tell;you</data>
the wanted, correct result is produced:
<manydata>
<onedata>how</onedata>
<onedata>many</onedata>
<onedata>i</onedata>
<onedata>can</onedata>
<onedata>tell</onedata>
<onedata>you</onedata>
</manydata>
<xsl:template match="data">
<manydata>
<!--
empty <manydata/> will be generated,
if <data/> without child text()
-->
<xsl:apply-templates select="text()"/>
</manydata>
</xsl:template>
<xsl:template match="data/text()">
<!-- start point for recursion -->
<xsl:call-template name="tokenize-string">
<xsl:with-param name="separator" select="';'"/>
<xsl:with-param name="string" select="text()"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize-string">
<xsl:param name="separator"/>
<xsl:param name="string"/>
<xsl:variable name="string-before-separator"
select="substring-before( $string, $separator )"/>
<onedata>
<xsl:choose>
<!-- if $separator presents in $string take first piece -->
<xsl:when test="$string-before-separator">
<xsl:value-of select="$string-before-separator"/>
</xsl:when>
<!-- take whole $string, if no $separator in the $string -->
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</onedata>
<!-- recursive call, if separator was found -->
<xsl:if test="$string-before-separator">
<xsl:call-template name="tokenize-string">
<xsl:with-param name="separator" select="$separator"/>
<xsl:with-param name="string"
select="substring-after( $string, $separator )"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="data">
<xsl:element name="manydata">
<xsl:call-template name="splitsemicolons">
<xsl:with-param name="text" select="text()" />
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="splitsemicolons">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text,';')">
<xsl:element name="onedata">
<xsl:value-of select="substring-before($text,';')" />
</xsl:element>
<xsl:call-template name="splitsemicolons">
<xsl:with-param name="text" select="substring-after($text,';')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:element name="onedata">
<xsl:value-of select="$text" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This uses a named template that is called recursively, each time outputting what's before the first ;, and calling itself with everything after the first ;. If there isn't a ;, it just outputs whatever's left as-is.
You can use the XSLT extension library to get the tokenize function. Here is how the final code will look like:
<xsl:template match="/">
<manydata>
<xsl:for-each select="str:tokenize(data, ';')">
<xsl:value-of select="." />
</xsl:for-each>
</manydata>
</xsl:template>
</xsl:stylesheet>
Please note you will have to import the extension library into you XSLT using:
<xsl:import href="path/str.xsl" />
before you use the library functions.