XML Tutorial/XSLT stylesheet/translate
Версия от 18:22, 25 мая 2010; (обсуждение)
How to find out that some text starts with a number.
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<value>3aacc</value>
<value>9-12-45</value>
<value>Q6-88</value>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//value"/>
</xsl:template>
<xsl:template match="value">
<Paragraph>
<xsl:value-of select="."/>
<xsl:if test="starts-with(translate(., "0123456789", "9999999999"), "9")">
<xsl:text> (the text starts with a number)</xsl:text>
</xsl:if>
</Paragraph>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><Paragraph>3aacc (the text starts with a number)</Paragraph><Paragraph>9-12-45 (the text starts with a number)</Paragraph><Paragraph>Q6-88</Paragraph>
Use translate to code a string
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<text>test</text>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Paragraph>
<xsl:value-of select="//text"/>
</Paragraph>
<Paragraph>
<xsl:value-of select="translate(//text,"egos","EGOS")"/>
</Paragraph>
<Paragraph>
<xsl:value-of select="translate(//text,"se","d")"/>
</Paragraph>
<Paragraph>
<xsl:value-of select="translate(//text,"gseo","bad")"/>
</Paragraph>
<Paragraph>
<xsl:value-of select="translate(//text,"gseg","bksC")"/>
</Paragraph>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><Paragraph>test</Paragraph><Paragraph>tESt</Paragraph><Paragraph>tdt</Paragraph><Paragraph>tdat</Paragraph><Paragraph>tskt</Paragraph>