2

我是XSL-FO的新手,有一个非常基本的问题,以下是 xsl 和 xml 文件。
我希望为每个 xml 节点“inhouse”(模板匹配)输出“xxx”。但我不明白。

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:fo="http://www.w3.org/1999/XSL/Format">         
  <xsl:template match="CustomerData">    
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>        
        <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21.0cm" margin-left="1.5cm" margin-right="1.5cm" margin-top="0cm">
          <fo:region-body margin-top="3cm"/>
          <fo:region-before extent="0cm"/>
          <fo:region-after extent="0cm"/>     
        </fo:simple-page-master>
      </fo:layout-master-set>  
      <fo:page-sequence master-reference="simple">                                 
        <fo:flow flow-name="xsl-region-body">  
            <fo:block font-family="Arial" font-size="8.5pt" font-weight="normal">       
                abc                             
            </fo:block>         
             <xsl:template match="inhouse">  
                <fo:block color="#053679">
                      xxx            
              </fo:block>                 
           </xsl:template>
        </fo:flow>      
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CustomerData>
    <inhouse>   
    <customerList>
        <customer>           
            <name>Tom</name>
        </customer>
    </customerList>  
  </inhouse>
</CustomerData>
4

1 回答 1

1

这里的问题是您在 CustomerData 的模板匹配中有一个内部模板匹配

<xsl:template match="CustomerData">  
   ...
   <xsl:template match="inhouse">  
       ....
   </xsl:template>
   ....
</xsl:template>

不允许以这种方式嵌套模板。您需要做的是将您的内部模板移出当前模板,而是使用xsl:apply-templates告诉 XSLT 处理器在该点开始寻找其他模板。像这样的结构

<xsl:template match="CustomerData">  
   ...
   <xsl:apply-templates select="inhouse" />
   ...
</xsl:template>

<xsl:template match="inhouse">  
       ....
</xsl:template>

事实上,替换它可能会稍微好一点,<xsl:apply-templates select="inhouse" />因为这样可以处理您想要匹配的内部<xsl:apply-templates />元素以外的其他元素的情况。

试试这个 XSLT

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:fo="http://www.w3.org/1999/XSL/Format">         
  <xsl:template match="CustomerData">    
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>        
        <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21.0cm" margin-left="1.5cm" margin-right="1.5cm" margin-top="0cm">
          <fo:region-body margin-top="3cm"/>
          <fo:region-before extent="0cm"/>
          <fo:region-after extent="0cm"/>     
        </fo:simple-page-master>
      </fo:layout-master-set>  
      <fo:page-sequence master-reference="simple">                                 
        <fo:flow flow-name="xsl-region-body">  
            <fo:block font-family="Arial" font-size="8.5pt" font-weight="normal">       
                abc                             
            </fo:block>         
             <xsl:apply-templates />
        </fo:flow>      
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

   <xsl:template match="inhouse">  
       <fo:block color="#053679">
         xxx            
        </fo:block>                 
    </xsl:template>
</xsl:stylesheet>
于 2013-05-24T21:39:01.167 回答