繁体   English   中英

fop / xsl文件中的Html

[英]Html in an fop/xsl file

我正在使用FOP生成PDF。 PDF包含一个问卷部分,在XML标记之间有一些简单的HTML(即<p> <strong> <em> )。 我的解决方案是将模板应用于XSL文件中的这些标记,这些标记将对<p>标记之间的所有内容进行样式设置,依此类推。 我现在已经设置了一个基本模板,我相信我遇到了XSL文件如何在XML中找到<p>标签的问题。 我认为由于HTML标记位于另一个标记内,因此我的XSL无法按预期工作。 我想知道:你如何使用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标记上的正确位置? 如果没有,我应该在哪里指出它们? 谢谢!

目前,模板匹配具有匹配属性中元素的完整路径

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM