使用 xml 作为 xsl 变量

发布于 2024-09-27 21:48:22 字数 488 浏览 1 评论 0原文

我必须使用不同语言的两个文本块创建稍微动态的 pdf(两个变量)。

两个块中的大部分文本都是静态的,

我在想是否可以创建一个模板来为布局创建 xsl-fo。然后创建两个包含自定义 xml 的变量。就像:

<xsl:variable name="TEXT_CONTENT_ENG" >
  <STATIC_TEXT> 
   <LABEL>Hello</LABEL>
   <REQUEST>Please pay your bill before </REQUEST>
  </STATIC_TEXT>
</xsl:variable>

最后我可以使用这些变量应用创建的模板两次。

xsl 似乎使用给定变量进行验证,但我无法将模板应用于该 xml。尝试过,文档($TEXT_CONTENT_ENG)也不起作用。

这是否可能以及如何做到?

I have to create slightly dynamic pdf (two variables) with two text blocks in different languages.

Most of the text in both blocks is static

I was thinking if I could create one template that would create xsl-fo for the layout. Then create two variables containing custom xml. Something like:

<xsl:variable name="TEXT_CONTENT_ENG" >
  <STATIC_TEXT> 
   <LABEL>Hello</LABEL>
   <REQUEST>Please pay your bill before </REQUEST>
  </STATIC_TEXT>
</xsl:variable>

Finally I could apply created template twice using these variables.

xsl appears to validate with given variable but I couldn't apply template to that xml. Tried and also document($TEXT_CONTENT_ENG) neither worked.

Is this even possible and how to do it?

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

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

发布评论

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

评论(3

女皇必胜 2024-10-04 21:48:22

两个块中的大部分文本都是
静态

如果这是真的,那么正确的 XSLT 方式是内联数据。来自 http://www.w3.org/TR/xslt#stylesheet-element

此外,xsl:stylesheet
元素可以包含任何不包含的元素
来自 XSLT 命名空间,前提是
元素的扩展名称有一个
非空命名空间 URI。存在感
此类顶级元素不得
更改 XSLT 元素的行为
以及在此定义的函数
文档;例如,它不会是
允许这样的顶级元素
指定 xsl:apply-templates
是使用不同的规则来解决
冲突。因此,XSLT 处理器是
总是可以忽略这样的顶级
元素,并且必须忽略顶级
如果元素没有给出错误
无法识别名称空间 URI。
这些元素可以提供,
例如,

  • 扩展元素或扩展函数使用的信息(请参阅
    [14 个扩展]),

  • 有关如何处理结果树的信息,

  • 有关如何获取源树的信息,

  • 有关样式表的元数据,

  • 样式表的结构化文档。

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <variable name="vRTF">
        <STATIC_TEXT xmlns="">
            <LABEL>Hello</LABEL>
            <REQUEST>Please pay your bill before </REQUEST>
        </STATIC_TEXT>
    </variable>
    <template match="/">
        <apply-templates
              select="document('')/*/xsl:variable[@name='vRTF']/node()"
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
    </template>
    <template match="@*|node()">
        <copy>
            <apply-templates select="@* | node()"/>
        </copy>
    </template>
</stylesheet>

输出:

<STATIC_TEXT>
    <LABEL>Hello</LABEL>
    <REQUEST>Please pay your bill before </REQUEST>
</STATIC_TEXT>

注意:在 XML 1.0 中,您只能重置默认命名空间。

Most of the text in both blocks is
static

If this is true, then the proper XSLT way is inline data. From http://www.w3.org/TR/xslt#stylesheet-element

In addition, the xsl:stylesheet
element may contain any element not
from the XSLT namespace, provided that
the expanded-name of the element has a
non-null namespace URI. The presence
of such top-level elements must not
change the behavior of XSLT elements
and functions defined in this
document; for example, it would not be
permitted for such a top-level element
to specify that xsl:apply-templates
was to use different rules to resolve
conflicts. Thus, an XSLT processor is
always free to ignore such top-level
elements, and must ignore a top-level
element without giving an error if it
does not recognize the namespace URI.
Such elements can provide, for
example,

  • information used by extension elements or extension functions (see
    [14 Extensions]),

  • information about what to do with the result tree,

  • information about how to obtain the source tree,

  • metadata about the stylesheet,

  • structured documentation for the stylesheet.

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform">
    <variable name="vRTF">
        <STATIC_TEXT xmlns="">
            <LABEL>Hello</LABEL>
            <REQUEST>Please pay your bill before </REQUEST>
        </STATIC_TEXT>
    </variable>
    <template match="/">
        <apply-templates
              select="document('')/*/xsl:variable[@name='vRTF']/node()"
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
    </template>
    <template match="@*|node()">
        <copy>
            <apply-templates select="@* | node()"/>
        </copy>
    </template>
</stylesheet>

Output:

<STATIC_TEXT>
    <LABEL>Hello</LABEL>
    <REQUEST>Please pay your bill before </REQUEST>
</STATIC_TEXT>

Note: In XML 1.0 you can reset only default namespace.

辞别 2024-10-04 21:48:22

Alejandro 的答案总体上是正确的,但是命名空间的非常规使用有点令人困惑,而且他将数据包装在不必要的 xsl:variable 元素中,这也有点令人困惑。

只要将元素放入其自己的命名空间中,就可以使其成为 xsl:stylesheet 元素的子元素。然后,您可以使用 document('') 访问它,它返回当前的 XSLT 文档:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:in="urn:inline-data"
    exclude-result-prefixes="in"
>

   <in:TEXT_CONTENT_ENG>
      <STATIC_TEXT> 
         <LABEL>Hello</LABEL>
         <REQUEST>Please pay your bill before </REQUEST>
      </STATIC_TEXT>
   </in:TEXT_CONTENT_ENG>

   <xsl:template match="/">
      <output>
         <xsl:apply-templates 
             select="document('')/xsl:stylesheet/in:TEXT_CONTENT_ENG/*"/>
      </output>
   </xsl:template>

   <xsl:template match="STATIC_TEXT">
      <xsl:text>The label is </xsl:text>
      <xsl:value-of select="LABEL"/>
      <xsl:text> and the request is </xsl:text>
      <xsl:value-of select="REQUEST"/>
   </xsl:template>

</xsl:stylesheet>

Alejandro's answer is in general correct, but the unconventional use of namespaces is a little confusing, and he's wrapped the data in an unnecessary xsl:variable element, which is also a little confusing.

As long as you put your element in its own namespace, you can make it a child of the xsl:stylesheet element. You can then access it by using document(''), which returns the current XSLT document:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:in="urn:inline-data"
    exclude-result-prefixes="in"
>

   <in:TEXT_CONTENT_ENG>
      <STATIC_TEXT> 
         <LABEL>Hello</LABEL>
         <REQUEST>Please pay your bill before </REQUEST>
      </STATIC_TEXT>
   </in:TEXT_CONTENT_ENG>

   <xsl:template match="/">
      <output>
         <xsl:apply-templates 
             select="document('')/xsl:stylesheet/in:TEXT_CONTENT_ENG/*"/>
      </output>
   </xsl:template>

   <xsl:template match="STATIC_TEXT">
      <xsl:text>The label is </xsl:text>
      <xsl:value-of select="LABEL"/>
      <xsl:text> and the request is </xsl:text>
      <xsl:value-of select="REQUEST"/>
   </xsl:template>

</xsl:stylesheet>
尘曦 2024-10-04 21:48:22

使用 xalan 我可以这样做:

<xsl:apply-templates select="xalan:nodeset($TEXT_CONTENT_ENG)/STATIC_TEXT"/>

类似的功能也可用于 exslt

Using xalan I was able to do it like this:

<xsl:apply-templates select="xalan:nodeset($TEXT_CONTENT_ENG)/STATIC_TEXT"/>

Similar function is also available for exslt

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