Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ends with in XSLT v1.0

Tags:

I am trying to edit a current XSLT. The functionality I want is when the value of "//code_no" ends with 01 I want to edit the current city location. Currently this functionality does not exist. I have tried using string and substring but it gives me an error saying ends with functionality does not exist. Please help

The value coming from the xml is

<code_no> 1870410001 </code_no> 

in the xsl, I want to print this when the value ends with 01.

<td align="left" width="33%"><SPAN style="font-size: 12pt; font-family: Arial;">     <a> <b><u><xsl:value-of select="//city"/>, <xsl:value-of select="//state"/>      </u></b></a></SPAN></td> 
like image 307
user1582320 Avatar asked Aug 07 '12 15:08

user1582320


People also ask

How do you break a line in XSLT?

Include the attribute Method="text" on the xsl:output tag and include newlines in your literal content in the XSL at the appropriate points. If you prefer to keep the source code of your XSL tidy use the entity &#10; where you want a new line. Show activity on this post. if you write this in file e.g.

What is number () in XSLT?

Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

What are the two 2 inputs of an XSLT Processor?

The XSLT processor operates on two inputs: the XML document to transform, and the XSLT stylesheet that is used to apply transformations on the XML. Each of these two can actually be multiple inputs.


2 Answers

The XPath 1.0 equivalent of (the XPath 2.0) expression:

ends-with($s, $t) 

is:

$t = substring($s, string-length($s) - string-length($t) +1) 

You need just to substitute $s and $t in the last XPath expression with, respectively, the string to be tested, and the ending to be tested.

Here is a complete example:

<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="x|y">      <xsl:value-of select="name()"/> ends-with '_01': <xsl:value-of select=      "'_01' = substring(., string-length() - 2)"/> =============     </xsl:template> </xsl:stylesheet> 

when this transformation is applied on the following XML document (none was provided in the question!!!):

<t>  <x>abcd_01</x>  <y>abcd_11</y> </t> 

the wanted, correct result is produced:

x ends-with '_01': true =============     y ends-with '_01': false =============    
like image 173
Dimitre Novatchev Avatar answered Oct 13 '22 20:10

Dimitre Novatchev


I used

contains($string, $part) and normalize-space(substring-after($string, $part)) = '' 

Where

we are checking if $string ends with $part

like image 29
Jyotsna Sonawane Avatar answered Oct 13 '22 21:10

Jyotsna Sonawane