XPath函数——字符串函数字符串函数主要⽤来处理字符串。字符串函数主要包括以下:concat(),contains(),normalize-space(),substing(),substring-before(),subsring-after(),translate().
1、concat()
concat()函数⽤于串连多个字符串。
简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<e id="1">st</e>
<e id="2">nd</e>
<e id="3">rd</e>
</root>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>字符串函数php
<xsl:template match="/root">
<xsl:for-each select="e">
<xsl:value-of select="concat(@id,.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
结果:
1st
2nd
3rd
2、contains()
contains(str1,str2)函数⽤来判断str2是否是第⼀个字符串的⼀部分。
简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>XML</book>
<book>XSLT</book>
<book>XPath</book>
<book>C#</book>
</books>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>
<xsl:template match="/books">
<books>
<xsl:for-each select="book">
<xsl:if test="contains(.,'X')">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</books>
</xsl:template>
</xsl:stylesheet>
结果:
<?xml version="1.0" encoding="gb2312"?>
<books>
<book>XML</book>
<book>XSLT</book>
<book>XPath</book>
</books>
3、normalize-space()
normalize-space()⽤来将⼀个字符串的头部和尾部的空⽩字符删除,如果字符串中间含有多个连续的空⽩字符,将⽤⼀个空格来代替。简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<article>
<title> When The Wind Blows</title>
<paragraph>
When you have get ready for everything ,you could
Sleep though the wind blows
</paragraph>
<paragraph>
That means you should do your best on your work and fear
nothing
</paragraph>
</article>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>
<xsl:template match="/books">
<xsl:apply-templates select="article"/>
</xsl:template>
<xsl:template match="article">
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:value-of select="normalize-space()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果:
<?xml version="1.0" encoding="gb2312"?>
<article>
<title>When The Wind Blows</title>
<paragraph>When you have get ready for everything ,you could Sleep though the wind blows</paragraph>
<paragraph>That means you should do your best on your work and fear nothing</paragraph>
</article>
4、starts-with()
start-with(string,startr)函数⽤来判断string是否以startstr开头。
简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>XML</book>
<book>XSLT</book>
<book>XPath</book>
<book>C#</book>
</books>
xslt:
显⽰⾏号复制代码?<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>
<xsl:template match="/books">
<xsl:copy>
<xsl:for-each select="book">
<xsl:if test="starts-with(.,'X')">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果:
<?xml version="1.0" encoding="gb2312"?>
<books>
<book>XML</book>
<book>XSLT</book>
<book>XPath</book>
</books>
5、string-length()
string-length(string)函数⽤来返回参数string的长度,如果参数string为缺省,将返回上下⽂节点的字符串长度。
6、substring()
substring(string,number,length)函数⽤来截取字符串。参数string⽤于指定要截取的字符串;参数number⽤于指定开始位置;参数length ⽤于指定截取字符串的长度。如果缺少length参数将从开始位置
number⼀直到截取字符串的长度
简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>123456789ABCDEF</text>
</root>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>
<xsl:template match="/root">
<xsl:value-of select="substring(text,1,5)"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="substring(text,1)"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="substring(text,1,string-length(text))"/>
</xsl:template>
</xsl:stylesheet>
结果:
12345
123456789ABCDEF
123456789ABCDEF
7、substring-before()
substring-before(str1,str2)函数⽤于返回字符串str1中位于字符串str2之前的部分。
简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<datetime>
<date>2010-01-17</date>
<time>22:49:30</time>
</datetime>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>
<xsl:template match="/datetime">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="date">
<xsl:value-of select="concat(substring-before(.,'-'),'年')"/>
</xsl:template>
<xsl:template match="time">
<xsl:value-of select="concat(substring-before(.,':'),'时')"/>
</xsl:template>
</xsl:stylesheet>
结果:
2010年22时
8、substring-after()
substring-after(str1,str2)函数跟substring-before类似,substring-after0返回字符串str1中位于字符串str2之后的部分。简单⽰例:
xml:
<?xml version="1.0" encoding="UTF-8"?>
<dir>
<file&</file>
<file&l</file>
<file>t.xslt</file>
</dir>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:output method="xml" version="1.0" encoding="gb2312" indent="yes"/>
<xsl:template match="/dir">
<extends>
<xsl:for-each select="file">
<extend>
<xsl:value-of select="substring-after(.,'.')"/>
</extend>
</xsl:for-each>
</extends>
</xsl:template>
</xsl:stylesheet>
结果:
<?xml version="1.0" encoding="gb2312"?>
<extends>
<extend>txt</extend>
<extend>xml</extend>
<extend>xslt</extend>
</extends>
9、translate()
translate(string,replaced_txt,replacement_txt)函数⽤来替换字符串,替换string中的所有replaced_txt为replacement_txt.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论