How to replace single apostrophe to double apostrophe in XSLT 1.0? - xslt-1.0

I need to replace a single quote to double quotes.
For example: input = Ram's
So i need a output as Ram''s
How to do in xslt 1.0? Please help me!

Its working for me!
<xsl:template match="name">
<xsl:copy>
<xsl:call-template name="replace">
<!-- Here getting the replaced values -->
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="text"/>
<xsl:param name="searchString">'</xsl:param>
<xsl:param name="replaceString">''</xsl:param>
<xsl:choose>
<xsl:when test="contains($text,$searchString)">
<xsl:value-of select="substring-before($text,$searchString)"/>
<xsl:value-of select="$replaceString"/>
<!-- recursive call -->
<xsl:call-template name="replace">
<xsl:with-param name="text" select="substring-after($text,$searchString)"/>
<xsl:with-param name="searchString" select="$searchString"/>
<xsl:with-param name="replaceString" select="$replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Related

I want to convert XPath value like Z.12.s from Z12s using xslt

Please any one help me on converting XPath value to dot (full stop) separated.
E.g: Z12s to Z. 12.after 1st char need put dot and after 2 char dot then every 2 chars dot but not last value.
If you don't know the length of the input string, you will need to use a recursive named template for this, such as:
<xsl:template name="split-string">
<xsl:param name="string"/>
<xsl:param name="length" select="1"/>
<xsl:value-of select="substring($string, 1, $length)"/>
<xsl:if test="string-length($string) > $length">
<xsl:text>.</xsl:text>
<!-- recursive call -->
<xsl:call-template name="split-string">
<xsl:with-param name="string" select="substring($string, $length + 1)"/>
<xsl:with-param name="length" select="2"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Demo: https://xsltfiddle.liberty-development.net/pNmC4HF
If your address can be over 90 characters, add more if's to the AddressDetail template according the pattern that is in the template.
<xsl:template match="AddressDetails">
<xsl:copy>
<xsl:apply-templates select="node() | #*">
<xsl:with-param name="address" select="substring(Address, 1, 30)"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:if test="string-length(Address) > 30">
<xsl:copy>
<xsl:apply-templates select="node() | #*">
<xsl:with-param name="address" select="substring(Address, 31, 30)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:if>
<xsl:if test="string-length(Address) > 60">
<xsl:copy>
<xsl:apply-templates select="node() | #*">
<xsl:with-param name="address" select="substring(Address, 61, 30)"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:if>
<!-- etc. -->
</xsl:template>
<xsl:template match="Address">
<xsl:param name="address"/>
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:value-of select="$address"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>

Otherwise XSLT1.0 - correct output

I´ve a problem at my xslt.
Here it is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GRP">
<xsl:copy>
<!--copy the data from ADD - ST to the GRP so it can be used in the mapping to set the delivery address from end customer-->
<xsl:for-each select ="./ADD">
<xsl:if test="./QUALIFIER='ST'">
<xsl:copy-of select="PARTY_NAME_1"/>
<xsl:copy-of select="STREET_1"/>
<xsl:copy-of select="CITY"/>
<xsl:copy-of select="POSTAL_CODE"/>
<xsl:copy-of select="COUNTRY_CODE"/>
</xsl:if>
<xsl:choose>
<xsl:when test="./QUALIFIER='CN'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER"/>
</CONTACT_NUMBER>
</xsl:when>
<xsl:otherwise>
<xsl:if test="./QUALIFIER='ST'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER"/>
</CONTACT_NUMBER>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!--copy all other nodes-->
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Can you tell me, whats my mistake at my xml?
At my code i get Contact_Number 2 times.
I just like one of them... If Contact at CN - use this, otherwise use from ST.
-> If contact at CN and ST use CN.
Edit: xml:
<SEEDELFOR>
<GRP>
<ADD>
<QUALIFIER>CN</QUALIFIER>
<IDENTIFIER></IDENTIFIER>
<AGENCY_CODE></AGENCY_CODE>
<PARTY_NAME_1></PARTY_NAME_1>
<STREET_1></STREET_1>
<CITY></CITY>
<POSTAL_CODE></POSTAL_CODE>
<COUNTRY_CODE></COUNTRY_CODE>
<CONTACT>
<QUALIFIER></QUALIFIER>
<NUMBER>1111111</NUMBER>
</CONTACT>
</ADD>
<ADD>
<QUALIFIER>ST</QUALIFIER>
<IDENTIFIER></IDENTIFIER>
<AGENCY_CODE></AGENCY_CODE>
<PARTY_NAME_1></PARTY_NAME_1>
<STREET_1></STREET_1>
<CITY></CITY>
<POSTAL_CODE></POSTAL_CODE>
<CONTACT>
<QUALIFIER></QUALIFIER>
<NUMBER>2222222</NUMBER>
</CONTACT>
</ADD>
</GRP>
</SEEDELFOR>
How can i correct my xslt?
I hope you can help me.
Best regards
Julian
One of the way you can achieve this is, rewriting the template GRP as following:
<xsl:template match="GRP">
<xsl:copy>
<!--copy the data from ADD - ST to the GRP so it can be used in the mapping
to set the delivery address from end customer -->
<xsl:variable name="contact_ST">
<xsl:for-each select="./ADD">
<xsl:if test="./QUALIFIER='ST'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER" />
</CONTACT_NUMBER>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="contact_CN">
<xsl:for-each select="./ADD">
<xsl:if test="./QUALIFIER='CN'">
<CONTACT_NUMBER>
<xsl:value-of select="CONTACT/NUMBER" />
</CONTACT_NUMBER>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="./ADD">
<xsl:if test="./QUALIFIER='ST'">
<xsl:copy-of select="PARTY_NAME_1" />
<xsl:copy-of select="STREET_1" />
<xsl:copy-of select="CITY" />
<xsl:copy-of select="POSTAL_CODE" />
<xsl:copy-of select="COUNTRY_CODE" />
</xsl:if>
</xsl:for-each>
<xsl:choose>
<xsl:when test="$contact_CN != ''">
<xsl:copy-of select="$contact_CN" />
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$contact_ST" />
</xsl:otherwise>
</xsl:choose>
<!--copy all other nodes -->
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
Consider the following simplified example:
<xsl:template match="GRP">
<xsl:copy>
<!-- other code, if necessary-->
<xsl:variable name="cn" select="ADD[QUALIFIER='CN']" />
<xsl:variable name="st" select="ADD[QUALIFIER='ST']" />
<CONTACT_NUMBER>
<xsl:choose>
<xsl:when test="$cn">
<xsl:value-of select="$cn/CONTACT/NUMBER"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$st/CONTACT/NUMBER"/>
</xsl:otherwise>
</xsl:choose>
</CONTACT_NUMBER>
<!-- more code, if necessary-->
</xsl:copy>
</xsl:template>

I am trying to get XSL to render an XML attribute with an & tag as &amp

I have an XML tag which has names in it e.g. H&M. The requirement is that it should output as H &amp M. I am unable to achieve this yet.
None of the escape characters, & or & worked for me. Here's what worked:
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text, '&')">
<xsl:value-of select="substring-before($text,'&')"/>
<xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
<xsl:value-of select="substring-after($text,'&')"/>
</xsl:when>
<xsl:when test="contains($text, '&apos;')">
<xsl:value-of select="substring-before($text,'&apos;')"/>
<xsl:text disable-output-escaping="yes"><![CDATA[&apos;]]></xsl:text>
<xsl:value-of select="substring-after($text,'&apos;')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And then call template:
<Nm>
<xsl:variable name="suppNm">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="Payee/Name" />
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$suppNm" />
</Nm>
If anyone knows a better solution, would be very interested to know. I am a total beginner to XSL.

XSLT1.0 replace multiple occurences of char with <br />

Have the following template that I use with xsl:call-template, but I need to use it to replace a ~ with <br />. I can get it to work with none HTML replacements but not when I try to use <br /> or &NewLine; or
. Any suggestions:
<xsl:template name="replace-substring">
<xsl:param name="original"/>
<xsl:param name="substring"/>
<xsl:param name="replacement" select="''"/>
<xsl:variable name="first">
<xsl:choose>
<xsl:when test="contains($original, $substring)" >
<xsl:value-of select="substring-before($original, $substring)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$original"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="middle">
<xsl:choose>
<xsl:when test="contains($original, $substring)">
<xsl:value-of select="$replacement" />
</xsl:when>
<xsl:otherwise>
<xsl:text></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="last">
<xsl:choose>
<xsl:when test="contains($original, $substring)">
<xsl:choose>
<xsl:when test="contains(substring-after($original, $substring),
$substring)">
<xsl:call-template name="replace-substring">
<xsl:with-param name="original">
<xsl:value-of select="substring-after($original, $substring)"/>
</xsl:with-param>
<xsl:with-param name="substring">
<xsl:value-of select="$substring"/>
</xsl:with-param>
<xsl:with-param name="replacement">
<xsl:value-of select="$replacement"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after($original, $substring)" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="concat($first, $middle, $last)"/>
</xsl:template>
It's hard to tell what's going on with the first, middle, and last variables but you should be able to just use a literal <br/> in your param...
XML
<test>one~two~three</test>
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="test">
<xsl:copy>
<xsl:call-template name="replace-char"/>
</xsl:copy>
</xsl:template>
<xsl:template name="replace-char">
<xsl:param name="char" select="'~'"/>
<xsl:param name="replacement"><br/></xsl:param>
<xsl:param name="string" select="."/>
<xsl:variable name="remaining" select="substring-after($string,$char)"/>
<xsl:value-of select="substring-before(concat($string,$char),$char)"/>
<xsl:if test="contains($string,$char)">
<xsl:copy-of select="$replacement"/>
</xsl:if>
<xsl:if test="$remaining">
<xsl:call-template name="replace-char">
<xsl:with-param name="string" select="$remaining"/>
<xsl:with-param name="char" select="$char"/>
<xsl:with-param name="replacement" select="$replacement"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output
<test>one<br/>two<br/>three</test>

how to provide variable dynamic value from other template?

<xsl:template name="split">
<xsl:param name="list"/>
<xsl:variable name="first">
<xsl:value-of select="substring-before($list,' ')"/>
</xsl:variable>
<xsl:copy-of select="$first"/>
</xsl:template>
<xsl:variable name="test">c0 c1 c2 c3 c4</xsl:variable>
<xsl:variable name="var2>
<xsl:call-template name="split">
<xsl:with-param name="returnvalue">
<xsl:value-of select="$test"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
// processing done
i want to return value from template as c0 then back to template match do processing then again go to split template again return c1 done same processing then back to split template then again processing in match template; depending upon the value of test variable...
How could i retreive these values one by one and process the code..??
This stylesheet will scan forward over your input string:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:variable name="string" select="'c0 c1 c2 c3 c4'"/>
<xsl:variable name="delim" select="' '"/>
<xsl:template match="/">
<xsl:call-template name="wrapper">
<xsl:with-param name="string" select="$string"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="wrapper">
<xsl:param name="string"/>
<xsl:choose>
<!-- handle empty input -->
<xsl:when test=" $string = '' "/>
<!-- handle next token -->
<xsl:when test="contains($string, $delim)">
<xsl:call-template name="process">
<xsl:with-param name="substring" select="substring-before($string,$delim)"/>
</xsl:call-template>
<xsl:call-template name="wrapper">
<xsl:with-param name="string" select="substring-after($string,$delim)"/>
</xsl:call-template>
</xsl:when>
<!-- handle last token -->
<xsl:otherwise>
<xsl:call-template name="process">
<xsl:with-param name="substring" select="$string"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="process">
<xsl:param name="substring"/>
<xsl:text>RECEIVED SUBSTRING: </xsl:text>
<xsl:value-of select="$substring"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
to produce the following output:
RECEIVED SUBSTRING: c0
RECEIVED SUBSTRING: c1
RECEIVED SUBSTRING: c2
RECEIVED SUBSTRING: c3
RECEIVED SUBSTRING: c4