繁体   English   中英

有没有办法在使用xsl:apply-template和xsl:call-template时根据XML代码按顺序生成pdf?

[英]Is there a way to generate pdf in order based on the XML code when using xsl:apply-template and xsl:call-template?

我目前正在使用XML代码和XSLT在Java中生成多页PDF文档。

XML的生成顺序与Input的顺序相同(这就是我想要的)。

但是,在生成PDF时,我遇到组织问题。

由于使用call-template和apply-template调用了多个样式表,因此它按照我的应用模板类别的顺序生成PDF。

为了更好地解释这个是一个简化的例子:

Input:            
cheese
milk
bread
bagels
rice
eggs

鉴于此输入,我生成匹配订单的XML。

<food>
      <dairy>
          <cheese>
                   <.....></>   <---------cheese information
                   <.....></>    <---------etc.
          </cheese>
     </dairy>



      <grains>
          <bread>
                   <.....></>    <---------bread information
                   <.....></>    <---------etc.
          </bread>
     </grains>
 </food>

然后,样式表使用XML代码以下列方式生成PDF

<xsl:apply-templates select="/food/dairy"/>
<xsl:apply-templates select="/food/grains"/>

<xsl:template match="/food/dairy">
      <xsl:call-template name="dairy"></xsl:call-template>
</xsl:template>
<xsl:template match="/food/grains">
      <xsl:call-template name="grains"></xsl:call-template>
</xsl:template>

现在发生的事情是我们让鸡蛋跳过谷物,跟着奶酪和牛奶,而不是按照同样的顺序。

有没有办法在调用和应用模板时维护XML顺序?

我只能找到这个相关的帖子: 应用XSLT模板,尊重XML源中的顺序

虽然它不太匹配我的问题。

我觉得你的问题很混乱。 在您的示例中,匹配dairy的模板在模板匹配grains之前应用。 要防止这种情况并保持文档顺序,您应该替换:

<xsl:apply-templates select="/food/dairy"/>
<xsl:apply-templates select="/food/grains"/>

有:

<xsl:apply-templates select="/food/dairy | /food/grains"/>

当然,这是一个完全虚假的例子 - 只是因为XML文档不能有两个根元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM