2016-04-14 69 views
1

我正在使用FOP生成PDF。 PDF包含一個問卷部分,該部分在XML標籤之間有一些簡單的HTML(即<p> <strong> <em>)。我的解決方案是將模板應用於XSL文件中的這些標籤,這些標籤可以設置<p>標籤之間的所有內容等等。我現在設置了一個基本模板,我相信我遇到了XSL文件在XML中找到<p>標籤的問題。我認爲,由於HTML標籤位於另一個標籤內,因此我的XSL無法按預期工作。我想知道:如何使用XSL模板來設計這些內部HTML標籤?fp/xsl文件中的Html

XML生成如下。您可以在<QuestionText>標籤內看到HTML標籤。

<xml> 
    <Report> 
     <Stuff> 
      <BasicInfo> 
       <InfoItem> 
        <InfoName>ID</InfoName> 
        <InfoData>555</InfoData> 
       </InfoItem> 
       <InfoItem> 
        <InfoName>Name</InfoName> 
        <InfoData>Testing</InfoData> 
       </InfoItem> 
      </BasicInfo> 

      <SubReports title="Employee Reports"> 
       <SubReport> 
        <Question> 
         <QuestionText> 
          <p>1. Are the pads secure?</p> 
         </QuestionText> 
         <AnswerText>Yes</AnswerText> 
        </Question> 
        <Question> 
         <QuestionText> 
          <p> 
           2. <strong>Are the pads in good shape?</strong>&nbsp; 
          </p> 
         </QuestionText> 
         <AnswerText>Yes</AnswerText> 
        </Question> 
       </SubReport> 
      </SubReports> 
     </Stuff> 
    </Report> 
</xml> 

然後,我有一個XSL模板樣式的XML

<xsl:template match="Stuff"> 
    <fo:block> 
    <fo:block font-size="32pt" text-align="center" font-weight="bold">Report</fo:block> 
    </fo:block> 
<xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="SubReports"> 
    <xsl:for-each select="SubReport"> 
    <xsl:for-each select="Question"> 
     <fo:block xsl:use-attribute-sets="question"><xsl:apply-templates /></fo:block> 
     <fo:block xsl:use-attribute-sets="answer"><xsl:value-of select="AnswerText" /></fo:block> 
    </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="SubReports/SubReport/Question/QuestionText"> 
    <fo:block><xsl:apply-templates /></fo:block> 
</xsl:template> 

<xsl:template match="SubReports/SubReport/Question/QuestionText/p"> 
    <fo:block padding-before="10pt"><xsl:apply-templates /></fo:block> 
</xsl:template> 

<xsl:template match="SubReports/SubReport/Question/QuestionText/strong"> 
    <fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline> 
</xsl:template> 

<xsl:template match="SubReports/SubReport/Question/QuestionText/em"> 
    <fo:inline font-style="italic"><xsl:apply-templates/></fo:inline> 
</xsl:template> 

是在xsl:templates指向上的HTML標記合適的位置匹配的標籤?如果不是,我應該在哪裏指出它們?謝謝!

回答

3

此刻,你的模板匹配具有完整路徑元素匹配屬性

<xsl:template match="SubReports/SubReport/Question/QuestionText/strong"> 

但這不會在它是p元素的子元素的情況下strong元素相匹配。現在,你可以做到這一點...

<xsl:template match="SubReports/SubReport/Question/QuestionText/p/strong"> 

但是,你實際上並不需要在這裏指定完整的路徑。除非你想匹配一個非常具體的元素。這個模板匹配也可以做到這一點。

<xsl:template match="strong"> 

所以,你可以用這些代替

<xsl:template match="p"> 
    <fo:block padding-before="10pt"><xsl:apply-templates /></fo:block> 
</xsl:template> 

<xsl:template match="strong"> 
    <fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline> 
</xsl:template> 

<xsl:template match="em"> 
    <fo:inline font-style="italic"><xsl:apply-templates/></fo:inline> 
</xsl:template> 

您同時還使用xsl:apply-templates所以這個工作在多個嵌套的HTML代碼的情況下替換當前的模板。例如<p><strong><em>Test</em></strong></p>