简体   繁体   中英

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.

The XML is generated in the same order of the Input (which is what I want).

However, when generating the PDF I run into an organization issue.

Because of multiple Stylesheets being called using call-template and apply-template, It generates the PDF in the order of my apply-template categories.

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.

<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

<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?

I've only been able to find this relevant post: Apply XSLT template respecting the order in the XML-source

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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