XML/XSLT stylesheet/concat

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

concat function

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



String function: concat(), upper-case(), substring()

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?><CastList xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <CastMember>
    <Character><Name>name 1</Name></Character>
    <Actor><Name>actor 1</Name></Actor>
  </CastMember>
  <CastMember>
    <Character><Name>name 2</Name></Character>
    <Actor><Name>actor 2</Name></Actor>
  </CastMember>
</CastList>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="ul">
      <CastList><xsl:apply-templates /></CastList>
    </xsl:template>
    
    <xsl:template match="li">
      <CastMember><xsl:apply-templates /></CastMember>
    </xsl:template>
    
    <xsl:template match="span">
      <xsl:variable name="name" as="xs:string" 
                    select="concat(upper-case(substring(@class, 1, 1)),
                                   substring(@class, 2))" />
      <xsl:element name="{$name}">
        <Name><xsl:value-of select="." /></Name>
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  
    name 1
    actor 1
  
  
    name 2
    actor 2