问题描述
我必须用不同语言的两个文本块创建稍微动态的 pdf(两个变量).
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
我在想是否可以创建一个模板来为布局创建 xsl-fo.然后创建两个包含自定义 xml 的变量.类似的东西:
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 似乎使用给定的变量进行验证,但我无法将模板应用于该 xml.尝试并记录($TEXT_CONTENT_ENG)都不起作用.
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?
推荐答案
Alejandro 的回答大体上是正确的,但是命名空间的非常规使用有点令人困惑,他将数据包装在一个不必要的 xsl:variable
元素,这也有点令人困惑.
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.
只要将元素放在自己的命名空间中,就可以将其设为 xsl:stylesheet
元素的子元素.然后您可以使用 document('')
访问它,它返回当前的 XSLT 文档:
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>
这篇关于使用 xml 作为 xsl 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!