XSL 重用?是的!但是:元素不得包含 xsl:import 元素! :-(

发布于 2024-08-30 01:55:21 字数 1215 浏览 8 评论 0原文

我使用的是带有大量重复转换的重型样式表,因此我认为重用相同的代码块是明智的,这样我就不需要在许多不同的地方进行相同的更改。所以我发现了,但是-唉-它不允许我这样做。当尝试在 Sonic Workbench 中运行它时,出现以下错误:

xsl:for-each 元素不得包含 xsl:import 元素

这是我的样式表代码:

<xsl:template match="/">
  <InboundFargoMessage>
   <EdiSender>
    <xsl:value-of select="TransportInformationMessage/SenderId"/>
   </EdiSender>
   <EdiReceiver>
    <xsl:value-of select="TransportInformationMessage/RecipientId"/>
   </EdiReceiver>
      <EdiSource>PORLOGIS</EdiSource>
      <EdiDestination>FARGO</EdiDestination>
   <Transportations>
    <xsl:for-each select="TransportInformationMessage/TransportUnits/TransportUnit">
     <xsl:import href="TransportCDMtoFDM_V0.6.xsl"/>
    </xsl:for-each>
    <xsl:for-each select="TransportInformationMessage/Waybill/TransportUnits/TransportUnit">
     <xsl:import href="TransportCDMtoFDM_V0.6.xsl"/>
    </xsl:for-each>
   </Transportations>
  </InboundFargoMessage>
 </xsl:template>
</xsl:stylesheet>

我现在将忽略子 xsl-sheets,因为问题似乎发生在基础上。

如果我无法使用 xsl:import,是否有重用的选项?

I am using a heavy stylesheet with a lot of recurring transformations, so I thought it would be smart to reuse the same chunks of code, so I would not need to make the same changes at a bunch of different places. So I discovered , but -alas- it won't allow me to do it. When trying to run it in Sonic Workbench I get the following error:

An xsl:for-each element must not contain an xsl:import element

This is my stylesheet code:

<xsl:template match="/">
  <InboundFargoMessage>
   <EdiSender>
    <xsl:value-of select="TransportInformationMessage/SenderId"/>
   </EdiSender>
   <EdiReceiver>
    <xsl:value-of select="TransportInformationMessage/RecipientId"/>
   </EdiReceiver>
      <EdiSource>PORLOGIS</EdiSource>
      <EdiDestination>FARGO</EdiDestination>
   <Transportations>
    <xsl:for-each select="TransportInformationMessage/TransportUnits/TransportUnit">
     <xsl:import href="TransportCDMtoFDM_V0.6.xsl"/>
    </xsl:for-each>
    <xsl:for-each select="TransportInformationMessage/Waybill/TransportUnits/TransportUnit">
     <xsl:import href="TransportCDMtoFDM_V0.6.xsl"/>
    </xsl:for-each>
   </Transportations>
  </InboundFargoMessage>
 </xsl:template>
</xsl:stylesheet>

I will leave out the child xsl-sheets for now, as the problem appears to be happening at the base.

If I cannot use xsl:import, is there any option of reuse?

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

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

发布评论

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

评论(3

水波映月 2024-09-06 01:55:21

如果我不能使用 xsl:import,有吗
有重用的选择吗?

可以使用

所有 元素必须是 的第一个元素子元素

作为替代方案, 元素必须是全局定义的( 的子元素),但前面可以是可全局放置的任何其他 xslt 指令。

您需要清楚并理解这两个 XSLT 指令的使用规则。我建议阅读一本关于 XSLT 的好书。

XSLT 中可重用性的主要单元是模板 ()。

导入样式表可以使用(通过 )在任何导入样式表中定义的任何模板。

If I cannot use xsl:import, is there
any option of reuse?

You can use <xsl:import>.

All <xsl:import> elements must be the first element children of <xsl:stylesheet>

As an alternative, an <xsl:include> element has to be globally defined (a child of <xsl:stylesheet>) but can be preceded by any other xslt instruction that can be placed globally.

You need to be aware of and understand well the rules of using these two XSLT instructions. I'd recommend reading a good book on XSLT.

The main unit of reusability in XSLT is the template (<xsl:template>).

The importing stylesheet can use (via <xsl:call-template> or <xsl:apply-templates>) any template that is defined in any imported stylesheet.

夏末的微笑 2024-09-06 01:55:21

每个包含的 XSL 文件都应包含一个模板。

主文件在开头包含其他文件,然后从各个位置使用 call-templateapply-templates 调用模板。

Each of included XSL files should contain a template(s).

The main file includes the others in the beginning and then calls the templates with call-template or apply-templates from various places.

一曲爱恨情仇 2024-09-06 01:55:21

感谢您的所有建议,这些建议有些帮助,但请允许我制定一个完整的答案。正如所建议的,重用问题的答案在于 xsl:templates。可以通过将模板包含在 .然后,只要有需要,就可以通过添加元素来召唤它们。此外,只要在父 xsl 工作表的顶部导入它们,就可以将它们放入单独的 xsl 工作表中。

因此,我的问题的解决方案如下所示:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:far="http://www.itella.com/fargo/fargogate/" xmlns:a="http://tempuri.org/XMLSchema.xsd" xmlns:p="http://tempuri.org/XMLSchema.xsd">
     <xsl:import href="TransportCDMtoFDM_V0.6.xsl"/>
     <xsl:template match="/">
        <InboundFargoMessage>
        <EdiSender>
            <xsl:value-of select="TransportInformationMessage/SenderId"/>
        </EdiSender>
        <EdiReceiver>
            <xsl:value-of select="TransportInformationMessage/RecipientId"/>
        </EdiReceiver>
        <EdiSource>PORLOGIS</EdiSource>
        <EdiDestination>FARGO</EdiDestination>
        <Transportations>
            <xsl:for-each select="TransportInformationMessage/TransportUnits/TransportUnit">
                <xsl:call-template name="transport"/>
            </xsl:for-each>
            <xsl:for-each select="TransportInformationMessage/Waybill/TransportUnits/TransportUnit">
                <xsl:call-template name="transport"/>
            </xsl:for-each>
        </Transportations>
    </InboundFargoMessage>
 </xsl:template>

文件“TransportCDMtoFDM_V0.6.xsl”包含名为“transport”的模板。

只剩下一个问题:使用模板时,模板中提到的所有节点都会被使用,即使它们是空的。那么剩下的问题就是如何去掉空节点呢?

Thanks for all the suggestions, which were somewhat helpful, but allow me formulate a complete answer. As suggested, the answer to the question of re-use lies in the xsl:templates. Templates can be defined by enclosing them within . Then, whereever necessary, they can be summoned by adding a element. Also, they can be put into separate xsl sheets, as long as they are imported at the top of the parent xsl sheet.

Thus, the solution to my questions looks as follows:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:far="http://www.itella.com/fargo/fargogate/" xmlns:a="http://tempuri.org/XMLSchema.xsd" xmlns:p="http://tempuri.org/XMLSchema.xsd">
     <xsl:import href="TransportCDMtoFDM_V0.6.xsl"/>
     <xsl:template match="/">
        <InboundFargoMessage>
        <EdiSender>
            <xsl:value-of select="TransportInformationMessage/SenderId"/>
        </EdiSender>
        <EdiReceiver>
            <xsl:value-of select="TransportInformationMessage/RecipientId"/>
        </EdiReceiver>
        <EdiSource>PORLOGIS</EdiSource>
        <EdiDestination>FARGO</EdiDestination>
        <Transportations>
            <xsl:for-each select="TransportInformationMessage/TransportUnits/TransportUnit">
                <xsl:call-template name="transport"/>
            </xsl:for-each>
            <xsl:for-each select="TransportInformationMessage/Waybill/TransportUnits/TransportUnit">
                <xsl:call-template name="transport"/>
            </xsl:for-each>
        </Transportations>
    </InboundFargoMessage>
 </xsl:template>

Where the file 'TransportCDMtoFDM_V0.6.xsl' contains the template called "transport".

There is just one problem left: Using templates, all the nodes mentioned within the template are used, even if they are empty. So the remaining question is how to leave out the empty nodes?

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