在 XSLT 中调用函数

Ste*_*ris 2 xml xslt xpath

我尝试在我自己的样式表中运行以下链接的功能之一。但我不知道如何。

这是一个xsltransform.net 演示

这是我要运行的功能:

功能 1

功能 2

Mar*_*nen 5

假设使用像 Saxon 9 这样的 XSLT 2.0 处理器,您可以xsl:function按如下方式使用:

<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>
Run Code Online (Sandbox Code Playgroud)

使用一些 XSLT 1.0 处理器,例如 Saxon 6,我认为您可以使用 Xalan 或 XsltProc

<?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>
Run Code Online (Sandbox Code Playgroud)