在 XSLT 中调用函数 [英] Calling a function in XSLT
问题描述
我尝试在我自己的样式表中运行下面链接的功能之一.但我不知道如何.
这是一个 xsltransform.net 演示.
这里是我想运行的函数:
假设像 Saxon 9 这样的 XSLT 2.0 处理器,您可以使用 xsl:function
如下:
<xsl:output method="html" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="/"><div><ul><xsl:apply-templates/>
</xsl:模板><xsl:template match="xs:element"><li xPath="{func:generateXPath(.)}"><xsl:value-of select="@name"/><xsl:if test="xs:*"><ul><xsl:apply-templates/></xsl:if></xsl:模板><xsl:function name="func:generateXPath" as="xs:string" ><xsl:param name="pNode" as="node()"/><xsl:value-of select="$pNode/ancestor-or-self::*/name()" separator="/"/></xsl:function></xsl:stylesheet>
使用一些 XSLT 1.0 处理器,例如 Saxon 6,我认为您可以使用 Xalan 或 XsltProc
<xsl:output method="html" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="/"><div><ul><xsl:apply-templates/>
</xsl:模板><xsl:template match="xs:element"><li xPath="{mf:getXpath()}"><xsl:value-of select="@name"/><xsl:if test="xs:*"><ul><xsl:apply-templates/></xsl:if></xsl:模板><func:function name="mf:getXpath"><xsl:variable name="xpath"><xsl:for-each select="ancestor-or-self::*"><xsl:value-of select="name()"/><xsl:if test="not(position()=last())"><xsl:value-of select="'/'"/></xsl:if></xsl:for-each></xsl:变量><func:result select="$xpath"/></func:function></xsl:transform>
I try to run one of the functions which are linked bellow in my own stylesheet. But I dont know how.
Here is an xsltransform.net demo.
And here are the functions I want to run:
Assuming an XSLT 2.0 processor like Saxon 9 you can use xsl:function
as follows:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:func="http://example.com/mf">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<div>
<ul>
<xsl:apply-templates/>
</ul>
</div>
</xsl:template>
<xsl:template match="xs:element">
<li xPath="{func:generateXPath(.)}">
<xsl:value-of select="@name"/>
<xsl:if test="xs:*">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:if>
</li>
</xsl:template>
<xsl:function name="func:generateXPath" as="xs:string" >
<xsl:param name="pNode" as="node()"/>
<xsl:value-of select="$pNode/ancestor-or-self::*/name()" separator="/"/>
</xsl:function>
</xsl:stylesheet>
With some XSLT 1.0 processors like Saxon 6 and I think Xalan or XsltProc you can use
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:func="http://exslt.org/functions"
xmlns:mf="http://example.com/mf"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="func mf xs">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<div>
<ul>
<xsl:apply-templates/>
</ul>
</div>
</xsl:template>
<xsl:template match="xs:element">
<li xPath="{mf:getXpath()}">
<xsl:value-of select="@name"/>
<xsl:if test="xs:*">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:if>
</li>
</xsl:template>
<func:function name="mf:getXpath">
<xsl:variable name="xpath">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()"/>
<xsl:if test="not(position()=last())">
<xsl:value-of select="'/'"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<func:result select="$xpath" />
</func:function>
</xsl:transform>
这篇关于在 XSLT 中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!