在XSLT中使用函数

时间:2011-05-04 12:06:18

标签: xslt xpath functx

我正在学习XSLT。这些问题可能很明显,但我现在真的被困住了。 Oxygen返回以下两种错误:

  1.   

    未为'ownFunction()'声明命名空间。 (“未声明的名称空间前缀{xs}”)

  2. 未知系统函数index-of-string()
    我从this website得到的XSLT函数index-of-string似乎无法被识别

  3. 这是XSL文件的简化版本:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"     xmlns:foo="http://www.wathever.com">
    <xsl:output method="xml" />
    
      <xsl:function name="foo:ownFunction" as="xs:string">
        <xsl:param name="string" as="xs:string"/>
    
            <xsl:choose>
    
              <xsl:when test='contains($string,"src=")'>
                <xsl:variable name="position"><xsl:value-of select="index-of-string($string,'src=')"/>+<xsl:number value="10"/></xsl:variable>
                <xsl:variable name="partString"><xsl:value-of select="substring($string,$position)"/></xsl:variable>
                <xsl:variable name="length"><xsl:value-of select="index-of-string($partString,'quot;')"/> - <xsl:number value="2"/></xsl:variable>
                <xsl:value-of select="substring($partString,1,$length)"/>
              </xsl:when>
    
              <xsl:otherwise>
                <xsl:value-of select="hotpot-jmatch-file/data/title"/>
              </xsl:otherwise>
    
            </xsl:choose>
      </xsl:function>
    
      <xsl:template match="/">
        <data>
          <title>
            <xsl:variable name="string"><xsl:value-of select="hotpot-jmatch-file/data/title"/></xsl:variable>
            <xsl:value-of select="foo:ownFunction($string)"/>
          </title>
        </data>
      </xsl:template>
    </xsl:stylesheet>
    

2 个答案:

答案 0 :(得分:3)

  

氧气返回以下两种   错误:

     

1)未声明命名空间   'ownFunction()'。 (“未申报   名称空间前缀{xs}“)

这实际上是一个XML问题。任何XSLT样式表myst都是格式良好的XML文档。在格式良好的其他要求中,使用的任何名称空间前缀必须绑定到名称空间声明中的名称空间URI。

要更正此问题,请将"xs"前缀绑定到"http://www.w3.org/2001/XMLSchema" - 这意味着将xmlns:xs="http://www.w3.org/2001/XMLSchema"添加到元素中(通常顶部元素是此命名空间的不错选择。

"foo:ownFunction"存在同样的问题。在使用前,您必须具有绑定/定义且可见的前缀"foo"。只需将xmlns:foo="my:foo"添加到样式表的顶部元素即可。

  

2)“未知的系统功能   index-of-string()“。XSLT函数   “索引字符串”我是从这里得到的   网站似乎不是   确认:   http://www.xsltfunctions.com/xsl/functx_index-of-string.html

您忘记要么从Priscilla Walmsley的网站复制并粘贴该功能,要么将其保存在单独的文件中(推荐),然后使用<xsl:import><xsl:include>导入/包含此样式表文件到你的转型。

最后,这些问题表明您需要更系统地介绍XSLT。得到一本好书并阅读它。你不会后悔的。 This answer 可能有助于列出我认为优秀的XSLT和XPath学习资源。

答案 1 :(得分:1)

使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"     xmlns:foo="http://www.wathever.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs functx""
xmlns:functx="http://www.functx.com">

<xsl:import href="location-of-functx-library.xsl"/>

...

<xsl:value-of select="functx:index-of-string($partString,'quot;')"/>

该示例定义了模式名称空间并将其绑定到前缀xs,定义了您链接到的函数库的名称空间。您还需要下载函数库实现并将其导入,如图所示。