【问题标题】:Calling a function in XSLT在 XSLT 中调用函数
【发布时间】:2016-01-14 16:00:25
【问题描述】:

我尝试在我自己的样式表中运行下面链接的函数之一。但我不知道怎么做。

这是一个xsltransform.net demo

这是我要运行的功能:

func 1

func 2

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    假设像 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>
    

    对于一些 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>
    

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多