[英]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.