I. XSLT 1.0解決方案:
這XSLT 1.0轉換任何網址運作正常,而無需對哈氏一個共同的出發字符串的所有URL的任何假設:
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://libx.org/libx2/libapps</id>
<entry>
<id>http://libx.org/libx2/libapps/2</id>
</entry>
<entry>
<id>http://libx.org/libx2/libapps/3</id>
</entry>
</feed>
的:當所提供的XML文檔應用
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/2005/Atom">
<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="x:feed/x:id/node()"/>
<xsl:template match="x:entry/x:id/text()" name="eatSlashes">
<xsl:param name="pText" select="."/>
<xsl:choose>
<xsl:when test="not(contains($pText, '/'))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="eatSlashes">
<xsl:with-param name="pText" select=
"substring-after($pText, '/')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
想要產生正確的結果:
<feed xmlns="http://www.w3.org/2005/Atom">
<id/>
<entry>
<id>2</id>
</entry>
<entry>
<id>3</id>
</entry>
</feed>
二, XSLT 2.0溶液:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/2005/Atom">
<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="x:feed/x:id/node()"/>
<xsl:template match="x:entry/x:id/text()">
<xsl:sequence select="tokenize(.,'/')[last()]"/>
</xsl:template>
</xsl:stylesheet>
當對相同的XML文檔(以上)應用中,相同的正確的結果產生:
<feed xmlns="http://www.w3.org/2005/Atom">
<id/>
<entry>
<id>2</id>
</entry>
<entry>
<id>3</id>
</entry>
</feed>
好問題,1。是的,這在XSLT 1.0中很容易,在XSLT 2.0中很簡單。 –