我应该将 XSL 函数放在 XSL 文档中的什么位置?

发布于 2024-08-03 16:02:45 字数 1322 浏览 2 评论 0原文

我有一个 XSL 样式表,需要使用 xsl:function 添加一些自定义字符串操作。但我在尝试找出将该函数放在文档中的位置时遇到困难。

我的 XSL 简化版看起来像这样,

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:my="myFunctions" xmlns:d7p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="Master.xslt"/>
  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <!-- starts actual layout -->
      <fo:page-sequence master-reference="first">
        <fo:flow flow-name="xsl-region-body">
          <!-- this defines a title level 1-->
          <fo:block xsl:use-attribute-sets="heading">
            HelloWorld
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

我想放入一个简单的函数,

  <xsl:function name="my:helloWorld">
    <xsl:text>Hello World!</xsl:text>
  </xsl:function>

但是我无法确定将该函数放在哪里,当我将它放在节点下时,我收到一条错误消息“xsl:function”不能是“xsl:stylesheet”元素的子元素。,如果我将其放在该节点下,则会出现类似的错误。

我应该把函数放在哪里?理想情况下,我想将函数放在外部文件中并将它们导入到我的 xsl 文件中。

I have an XSL style sheet for which I need to add some custom string manipulation using an xsl:function. But I am having trouble trying to work out where to put the function in my document.

My XSL simplified looks like this,

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:my="myFunctions" xmlns:d7p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="Master.xslt"/>
  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <!-- starts actual layout -->
      <fo:page-sequence master-reference="first">
        <fo:flow flow-name="xsl-region-body">
          <!-- this defines a title level 1-->
          <fo:block xsl:use-attribute-sets="heading">
            HelloWorld
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

And I want to put in a simple function, say,

  <xsl:function name="my:helloWorld">
    <xsl:text>Hello World!</xsl:text>
  </xsl:function>

But I cannot work out where to put the function, when I put it under the node I get an error saying 'xsl:function' cannot be a child of the 'xsl:stylesheet' element., and if I put it under the node I get a similar error.

Where should I put the function? Idealy I would like to put my functions in an external file and import them into my xsl files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2024-08-10 16:02:46

您可以将样式表版本升级到2.0
然后在样式表声明中指定为

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://www.**.com"> 

** 您的选择,您可以根据需要指定任何内容
然后在下面指定您的函数

<xsl:function name="func:helloWorld">
  <xsl:text>Hello World!</xsl:text>
</xsl:function>

然后在模板中您可以将其用作

<xsl:template match="/">
<xsl:value-of select="func:helloWorld"/>
</xsl:template>

You can upgrade the stylesheet version to 2.0
Then in stylesheet declaration specify as

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://www.**.com"> 

** Your choice you can specify anything as your wish
then below this specify your function

<xsl:function name="func:helloWorld">
  <xsl:text>Hello World!</xsl:text>
</xsl:function>

Then in template you can use it as

<xsl:template match="/">
<xsl:value-of select="func:helloWorld"/>
</xsl:template>
沦落红尘 2024-08-10 16:02:45

XSL 1.0 版中没有 xsl:function。您必须创建一个命名模板

<xsl:template name="helloWorld">
  <xsl:text>Hello World!</xsl:text>
</xsl:template>

(...)

<xsl:template match="something">
  <xsl:call-template name="helloWorld"/>
</xsl:template>

There is no xsl:function in XSL version 1.0. You have to create a named template

<xsl:template name="helloWorld">
  <xsl:text>Hello World!</xsl:text>
</xsl:template>

(...)

<xsl:template match="something">
  <xsl:call-template name="helloWorld"/>
</xsl:template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文