我正在生成一个PDF与FOP。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>
</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标记上的正确位置?如果没有,我应该把它们指向哪里?谢谢!
发布于 2016-04-14 16:08:09
目前,模板匹配具有match属性中元素的完整路径。
<xsl:template match="SubReports/SubReport/Question/QuestionText/strong">但是在strong元素是p元素的子元素的情况下,这将与它不匹配。现在你可以这么做..。
<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>
https://stackoverflow.com/questions/36627821
复制相似问题