简体   繁体   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?

I'm currently generating a multiple page PDF document in Java using XML code and XSLT. 我目前正在使用XML代码和XSLT在Java中生成多页PDF文档。

The XML is generated in the same order of the Input (which is what I want). XML的生成顺序与Input的顺序相同(这就是我想要的)。

However, when generating the PDF I run into an organization issue. 但是,在生成PDF时,我遇到组织问题。

Because of multiple Stylesheets being called using call-template and apply-template, It generates the PDF in the order of my apply-template categories. 由于使用call-template和apply-template调用了多个样式表,因此它按照我的应用模板类别的顺序生成PDF。

To explain this better here is a simplified example: 为了更好地解释这个是一个简化的例子:

Input:            
cheese
milk
bread
bagels
rice
eggs

Given this input, I generate XML matching the order. 鉴于此输入,我生成匹配订单的XML。

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



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

The XML code is then used by the Stylesheet to Generate a PDF in the following way 然后,样式表使用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>

Now what Is happening is we get eggs jumping grains and following cheese and milk instead of staying in the same order. 现在发生的事情是我们让鸡蛋跳过谷物,跟着奶酪和牛奶,而不是按照同样的顺序。

Is there a way to maintain the XML order when calling and applying templates? 有没有办法在调用和应用模板时维护XML顺序?

I've only been able to find this relevant post: Apply XSLT template respecting the order in the XML-source 我只能找到这个相关的帖子: 应用XSLT模板,尊重XML源中的顺序

Although it doesn't quite match my problem. 虽然它不太匹配我的问题。

I find your question rather confusing. 我觉得你的问题很混乱。 In your example, templates matching dairy are applied before templates matching grains . 在您的示例中,匹配dairy的模板在模板匹配grains之前应用。 To prevent this and keep the document order, you should replace: 要防止这种情况并保持文档顺序,您应该替换:

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

with: 有:

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

Of course, this is a completely bogus example - if only because an XML document cannot have two root elements. 当然,这是一个完全虚假的例子 - 只是因为XML文档不能有两个根元素。

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

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