How to detect a leading space in the text
I want to add s开发者_如何学Pythonome strings in the text right after a leading space. Any ideas how to detect the leading space? Thanks.
For example, I would like to add "def" in front of abc but after the leading space.
<AAA>
<CCC> abc</CCC>
</AAA>
Output should become: " defabc"
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[starts-with(.,' ')]">
<xsl:value-of select=
"concat(' ', 'def', substring(.,2))"/>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<AAA>
<CCC> abc</CCC>
</AAA>
produces the wanted, correct result:
<AAA>
<CCC> defabc</CCC>
</AAA>
Assuming, from your tag, that you are trying to do this in xslt, I'd use XSLT's starts-with function.
If you provide some example XSLT code, it'd be easier to explain more.
Besides Dimitre's answer with proper use of pattern matching, this XPath expression could help you:
concat(substring($AddString, 1 div starts-with($String,' ')), $String)
精彩评论