Html 在 fop/xsl 文件中

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

但是如果 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>