XML/XSLT stylesheet/translate

Материал из Web эксперт
Версия от 11:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

translate function

   <source lang="xml">


File: Data.xml <poem>

 <verse>line 1</verse>
 <verse>
   line 2
 </verse>

</poem> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 
 <xsl:template match="verse">
   <xsl:value-of select="concat("length: ",string-length(.))" />
   <xsl:if test="contains(.,"light")">
     <xsl:text>light: yes!</xsl:text>
   </xsl:if>
   <xsl:if test="starts-with(.,"Seest")">
     <xsl:text>Yes, starts with "Seest"</xsl:text>
   </xsl:if>
   <xsl:value-of select="normalize-space(.)" />
   <xsl:value-of select="translate(.,"abcde","ABCD")" />
 </xsl:template>
 

</xsl:stylesheet> Output:

 length: 6line 1lin 1
 length: 13line 2
   lin 2
 
</source>
   
  


Use translate() function and text() function to change the letter case

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <Employees>

 <Person>
   <FirstName>A</FirstName>
   <LastName>B</LastName>
   <DateOfBirth>2008-12-12</DateOfBirth>
 </Person>
 <Person>
   <FirstName>C</FirstName>
   <LastName>D</LastName>
   <DateOfBirth>2008-11-11</DateOfBirth>
 </Person>
 <Person>
   <FirstName>E</FirstName>
   <LastName>F</LastName>
   <DateOfBirth>2008-10-10</DateOfBirth>
 </Person>

</Employees>

File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
   <html>
     <body>
       <xsl:apply-templates
         select="//text()[normalize-space(.)]" />
     </body>
   </html>
 </xsl:template>
 <xsl:template match="text()">
   <xsl:variable name="upper"
     select=""ABCDEFGHIJKLMNOPQRSTUVWXYZ"" />
   <xsl:variable name="lower"
     select=""abcdefghijklmnopqrstuvwxyz"" />
   The input
   
     <xsl:value-of select="." />
   
   was translated to
   
     <xsl:value-of select="translate(., $lower, $upper)" />
     
</xsl:template>

</xsl:stylesheet> Output: <html>

  <body>
         The input
         A
         was translated to
         A
The input B was translated to B
The input 2008-12-12 was translated to 2008-12-12
The input C was translated to C
The input D was translated to D
The input 2008-11-11 was translated to 2008-11-11
The input E was translated to E
The input F was translated to F
The input 2008-10-10 was translated to 2008-10-10
</body>

</html>

</source>
   
  


Use translate function with if statement

   <source lang="xml">

File: Data.xml <poem>

 <a>line 1</a>
 line 1
 <c>line 1</c>
 <d>
   line 1
 </d>

</poem> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
 <xsl:template match="a">
   <xsl:if test=". = "line 1"">
     1. a = "line 1"
   </xsl:if>
   <xsl:if test=". = ../b">2. a = ../b</xsl:if>
   <xsl:if test=". = ../c">3. a = ../c</xsl:if>
   <xsl:if test=". != ../c">4. a != ../c</xsl:if>
   <xsl:if
     test="translate(.,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ") = translate(../c,"abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ")">
     5. a = ../c (ignoring case)
   </xsl:if>
   <xsl:if test=". = ../d">6. a = ../d</xsl:if>
   <xsl:if test=". = normalize-space(../d)">
     7. a = normalize-space(../d)
   </xsl:if>
 </xsl:template>
 <xsl:template match="b|c|d" />

</xsl:stylesheet> Output:


     1. a = "line 1"
   2. a = ../b3. a = ../c
     5. a = ../c (ignoring case)
   
     7. a = normalize-space(../d)
   
 
 
 
</source>