【问题标题】:XSLT-FO for-eachXSLT-FO for-each
【发布时间】:2017-04-27 07:35:39
【问题描述】:

这是我的 xml:

<OrdersSchedulePDFView>
 <OrdersSchedulePDFViewRow>
   <Locations>
     <LocName>Text1</LocName>
     <LocName>Text2</LocName>         
   </Locations>
 </OrdersSchedulePDFViewRow>     
</OrdersSchedulePDFView>

这是我的 xslt-fo 文件中的片段:

<xsl:template match="OrdersSchedulePDFView">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">        
        <xsl:for-each select="./OrdersSchedulePDFViewRow">        
        <fo:page-sequence master-reference="all">            
            <fo:flow flow-name="xsl-region-body">                       
                    <xsl:for-each select="Locations">                   
                     <xsl:apply-templates select="."/>                  
                    </xsl:for-each>                             
            </fo:flow>
        </fo:page-sequence>
        </xsl:for-each>
    </fo:root>
</xsl:template>
<xsl:template match="Locations">
    <fo:block text-align="left" font-family="Arial">
        <fo:inline font-weight="bold"><xsl:value-of select="LocName"/></fo:inline>
    </fo:block>  
</xsl:template>
</xsl:stylesheet>

但在 PDF 中,我只有一个 LocName。如何获取所有 LocName 元素?

【问题讨论】:

  • &lt;xsl:value-of select="LocName"/&gt; 仅获取第一个 LocName 的值(在 XSLT 1.0 中)。要获得所有这些,您需要使用&lt;xsl:for-each select="LocName"&gt;。或者对它们应用模板,并添加相应的模板。 --- 顺便说一句,您确实不需要需要使用&lt;xsl:for-each select="Locations"&gt; 才能应用匹配Locations 的模板。只需说&lt;xsl:apply-templates select="Locations"/&gt;
  • 最后,我想通了。

标签: xml apache xslt apache-fop


【解决方案1】:

根据您的更新:显然您没有为Locations 元素添加代码,您可以通过更改来缩短代码:

<xsl:apply-templates select="Locations"/>

到:

<xsl:apply-templates select="Locations/LocName"/>

然后做:

<xsl:template match="LocName"> 
    <fo:block text-align="left" font-family="Arial"> 
        <fo:inline font-weight="bold">
            <xsl:value-of select="."/>
        </fo:inline> 
    </fo:block> 
</xsl:template>

【讨论】:

    【解决方案2】:

    对我来说正确的代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">   
    <xsl:template match="OrdersSchedulePDFView">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">        
            <xsl:for-each select="./OrdersSchedulePDFViewRow">        
            <fo:page-sequence master-reference="all">            
                <fo:flow flow-name="xsl-region-body">
                        <fo:block font-weight="bold" background-color="black" color="white" padding="2pt" font-family="Arial">Stores</fo:block>
                        <fo:block text-align="left" font-family="Arial">
            <fo:inline font-weight="bold"><xsl:apply-templates select="Locations"/></fo:inline>
        </fo:block>                             
                </fo:flow>
            </fo:page-sequence>
            </xsl:for-each>
        </fo:root>
    </xsl:template>
    <xsl:template match="Locations">
        <xsl:for-each select="./LocName">
        <fo:block text-align="left" font-family="Arial">
            <fo:inline font-weight="bold"><xsl:value-of select="."/></fo:inline>
        </fo:block>
        </xsl:for-each> 
    </xsl:template>
    

    【讨论】:

    • 此代码是否适合您。我正在尝试实现类似的目标,但它对我不起作用。你的数据结构到底如何
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多