XPath函数——字符串函数(转载)
本文是转载的,原文网址:http://www.cnblogs.com/zhaozhan/archive/2010/01/17/1650242.html
字符串函数主要用来处理字符串。字符串函数主要包括以下: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="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" version="1.0" encoding="gb2312" indent="yes"/>
<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="http://www.w3.org/1999/XSL/Transform"> <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="http://www.w3.org/1999/XSL/Transform"> <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="http://www.w3.org/1999/XSL/Transform"> <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="http://www.w3.org/1999/XSL/Transform"> <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="http://www.w3.org/1999/XSL/Transform"> <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>a.txt</file>
<file>t.xml</file>
<file>t.xslt</file>
</dir>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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.
XPath函数——字符串函数(转载)的更多相关文章
- ylb:SQLServer常用系统函数-字符串函数、配置函数、系统统计函数
原文:ylb:SQLServer常用系统函数-字符串函数.配置函数.系统统计函数 ylbtech-SQL Server:SQL Server-SQLServer常用系统函数 -- ========== ...
- javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数
javascript函数一共可分为五类: ·常规函数 ·数组函数 ·日期函数 ·数学函数 ·字符串函数 1.常规函数 javascript常规函数包括以下9个 ...
- Sass函数--字符串函数
Sass的函数简介在 Sass 中除了可以定义变量,具有 @extend.%placeholder 和 mixins 等特性之外,还自备了一系列的函数功能.其主要包括: ● 字符串函数 ● 数字函数 ...
- mssql 系统函数-字符串函数专题--字符串函数大全
mssql 系统函数 字符串函数 substring 功能简介 mssql 系统函数 字符串函数 stuff 功能简介 mssql 系统函数 字符串函数 str 功能简介 mssql 系统函数 字符串 ...
- php常用函数——字符串函数
php常用函数——字符串函数
- 2016/3/17 Mysq select 数学函数 字符串函数 时间函数 系统信息函数 加密函数
一,数学函数主要用于处理数字,包括整型.浮点数等. ABS(X) 返回x的绝对值 SELECT ABS(-1)--返回1 CEll(X),CEILING(x) 返回大于或等于x的最小整数 SELEC ...
- Linux下常用函数-字符串函数
inux下常用函数-字符串函数 atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib ...
- sql server 系统常用函数:聚合函数 数学函数 字符串函数 日期和时间函数和自定义函数
一.系统函数 1.聚合函数 聚合函数常用于GROUP BY子句,在SQL Server 2008提供的所有聚合函数中,除了COUNT函数以外,聚合函数都会忽略空值AVG.COUNT.COUNT_BIG ...
- sqlserver 中常见的函数字符串函数
---字符中操作函数 UPPER(S) 将字符串统一为大写字母 SELECT UPPER('asasA') --ASASA LOWER(S) 将字符串统一为小写字母 SELECT LOWER('asa ...
随机推荐
- 每建一个Activity都要注册权限Manifest.xml
每建一个Activity都要注册权限Manifest.xml 但是有时候自动注册好了,注意!不然的话是不能调用的!!!!!<activity android:name=".MainVi ...
- mysql 主从复制配置
环境:已经在centos下安装好mysql,安装参考:http://www.cnblogs.com/bookwed/p/5896619.html,安装好主数据库后,可以克隆一份,注意修改ip等. 19 ...
- opentsdb basic install
git clone git://github.com/OpenTSDB/opentsdb.git cd opentsdb ./build.sh env COMPRESSION=NONE HBASE_H ...
- 今天主要推荐一下django学习的网址!
前言:每个月忙碌的头20天后,在上班时间投入到django理论学习的过程中,花了差不多3天时间简单的研究了一下django,着实废了我不少脑细胞. 采用虫师前辈的一张图和话: 如果你把这过程梳理清晰了 ...
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- Android原生游戏开发:使用JustWeEngine开发微信打飞机
使用JustWeEngine开发微信打飞机: 作者博客: 博客园 引擎地址:JustWeEngine 示例代码:EngineDemo JustWeEngine? JustWeEngine是托管在Git ...
- 【转】40个良好用户界面Tips
一个良好的用户界面应具有高转换率,并且易于使用.但要用户体验良好并不容易做到,下面我们整理了40个良好用户界面Tips,希望能对你有帮助! 1 尽量使用单列而不是多列布局 单列布局能够让对全局有更好的 ...
- 第3章 Linux常用命令(5)_网络命令和挂载命令
7. 网络命令 7.1 给用户发信息,以ctr+D保存结束 (1)write命令 命令名称 write 命令所在路径 /user/bin/write 执行权限 所有用户 语法 write <用户 ...
- Java — JTree and JTable以及sqlServer的两种连接
使用JTree的步骤: 暂时只能创建一个头结点,创建一个树的结点作为头结点(其子结点也是相同的创建方法):DefaultMutableTreeNode headNode = new DefaultMu ...
- 数据表格 - DataGrid - 行编辑
行编辑一般用于单行数据的增删改,如果不用行编辑实现的话,对于表单数据量不大的情况,可以使用弹窗(Dialog),如果数据量比较大,也就是需要操作的数据比较多的时候,可以新开一个tab页. 新增/编辑 ...