XML/XSLT stylesheet/contains

Материал из Web эксперт
Перейти к: навигация, поиск

contains 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>
   
  


match element with certain value

   <source lang="xml">

File: Data.xml <story>

 <chapter>
   <title>Chapter 1</title>
   <para>para 1</para>
 </chapter>
 <chapter>
   <title>Chapter 2</title>
   <para>item 1</para>
   <para>item 2</para>
   <sect>
     <title>Chapter 2, Section 1</title>
     <para>item 3</para>
     <para>para 2</para>
   </sect>
 </chapter>
 <chapter>
   <title>Chapter 3</title>
   <para>para A</para>
 </chapter>

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

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="chapter[contains(.,"the")]">
   *** This chapter has "the" in it: ***
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="chapter">
   *** This chapter element not processed by other template: ***
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="title" />

</xsl:stylesheet> Output:



   *** This para element not processed by other template: ***
   para 1
 
 
   
   
   *** This para element not processed by other template: ***
   item 1
   
   *** This para element not processed by other template: ***
   item 2
   
     
     
   *** This para element not processed by other template: ***
   item 3
     
   *** This para element not processed by other template: ***
   para 2
   
 
 
   
   
   *** This para element not processed by other template: ***
   para A
 
</source>
   
  


template match="para[contains(.,"the")]"

   <source lang="xml">

File: Data.xml <story>

 <chapter>
   <title>Chapter 1</title>
   <para>para 1</para>
 </chapter>
 <chapter>
   <title>Chapter 2</title>
   <para>item 1</para>
   <para>item 2</para>
   <sect>
     <title>Chapter 2, Section 1</title>
     <para>item 3</para>
     <para>para 2</para>
   </sect>
 </chapter>
 <chapter>
   <title>Chapter 3</title>
   <para>para A</para>
 </chapter>

</story>

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

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="para[contains(.,"the")]">
   *** This para has "the" in it: ***
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="para">
   *** This para element not processed by other template: ***
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="title" />

</xsl:stylesheet> Output:



   *** This para element not processed by other template: ***
   para 1
 
 
   
   
   *** This para element not processed by other template: ***
   item 1
   
   *** This para element not processed by other template: ***
   item 2
   
     
     
   *** This para element not processed by other template: ***
   item 3
     
   *** This para element not processed by other template: ***
   para 2
   
 
 
   
   
   *** This para element not processed by other template: ***
   para A
 
</source>