2017-04-27 64 views
2

這是我的xml:XSLT-FO的for-each

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

+0

''僅獲取第一個'LocName'(在XSLT 1.0中)的值。爲了獲得它們,你需要使用''。或者將模板應用於它們,並添加相應的模板。 ---順便說一句,你不**需要使用''爲了應用匹配'Locations'的模板。只要說''。 –

+0

最後,我明白了這一點。 的 \t <的xsl:for-每個選擇= 「./ LOCNAME 」> 的 \t的\t

回答

2

根據您的更新:因爲很明顯,你有沒有代碼,添加了對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> 
1

對我來說,正確的代碼:

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

DID此代碼爲工作您。我正在嘗試實現類似的功能,但它對我來說不起作用。你的數據結構究竟如何? –