使用XSL 2.0我想所有大写文本转换为具有在每个节点大写文字的第一个字母。 他们的大量可能的子元素。
<text> text text text
<head>BLAH <unkownTag>BLAH</unkownTag> BLAH </head>
</text>
我想改变这个阅读
<text> text text text
<head>Blah <unkownTag>Blah</unkownTag> Blah </head>
</text>
我来最接近的是
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="head/text()">
<xsl:value-of select="concat(upper-case(substring(.,1,1)),lower-case(substring(.,2)))"/>
</xsl:template>
这给我结果
<text> text text text
<head>Blah <unkownTag>BLAH</unkownTag> blah </head>
</text>
我怎样才能获得小写的转变发生在头部的所有子节点?
这种转变产生了划词标点符号的通缉的结果regardles:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="head//text()">
<xsl:analyze-string select="." regex="\p{{L}}+">
<xsl:matching-substring>
<xsl:value-of select=
"concat(upper-case(substring(.,1,1)), lower-case(substring(.,2)))"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
当应用所提供的XML文档:
<text> text text text
<head>BLAH <unkownTag>BLAH</unkownTag> BLAH </head>
</text>
在想,正确的结果产生:
<text> text text text
<head>Blah <unkownTag>Blah</unkownTag> Blah </head>
</text>
当应用这个XML文档上:
<text> text text text
<head>BLAH$<unkownTag>BLAH</unkownTag>-BLAH;</head>
</text>
再次正确的结果产生:
<text> text text text
<head>Blah$<unkownTag>Blah</unkownTag>-Blah;</head>
</text>
说明 :
正确使用的<xsl:analyze-string>
指令。
正确使用的\p{L}
字符类。
正确使用的<xsl:matching-substring>
和<xsl:non-matching-substring>
的指令。
在您的文本空间使这个一个有趣的问题。 要匹配所有文本()下面“头”节点,使用XPath表达式来看看祖先。
在这里,我在结果集改变的第一个字符为大写记号化字符串然后循环和下面的字符为小写。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[ ancestor::head ]">
<xsl:value-of select="
for $str in tokenize( ., '\s' )
return concat( upper-case(substring($str,1,1)),
lower-case(substring($str,2)) )"/>
</xsl:template>
</xsl:stylesheet>
试试这个。 我没有测试它,你可能需要调整它一下。
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="head">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:value-of select="concat(upper-case(substring(.,1,1)),lower-case(substring(.,2)))"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
这可以帮助你获得的方式的一部分。
<xsl:template match="/">
<xsl:apply-templates select="node()" mode="firstup"/>
</xsl:template>
<xsl:template match="text()" mode="firstup">
<!--<x>-->
<xsl:value-of select="concat(upper-case(substring(.,1,1)),lower-case(substring(.,2)))"/>
<!--</x>-->
</xsl:template>
不知道的第三个“嗒嗒”的是,这个文本()节点以空格开始,所以会有在获得同级文本节点资本纠正一些难度增加。 取消对“X”元素看到这一点。 你可能也想看看正常化空间和位置()函数来得到进一步。