需要一些帮助进行XML / XSLT转换

时间:2012-06-08 14:28:23

标签: html xslt transformation

我正在使用XSL-FO和FOP生成PDF。我正在将复杂的HTML页面转换为PDF。

我遇到了以下错误:

遇到未知格式化对象“{} br”(p的子节点)。 (无上下文)

FOP处理器不了解我提供的XSL-FO的格式,因为其中仍然有一些HTML标记。我想在下面链接的xml中过滤<p><br/>标记:

http://www.tekstenuitleg.net/xmlinput.xml

在最后一位,在“标签元素1”和“标签元素2”下,您可以看到FOP不理解的<p><br/>

你能帮我用XSLT过滤掉这些并用<fo:block>some replacement here</fo:block>替换它们吗?我尝试了许多不同的XSLT样式表,但它们并不常用。我将XSLT恢复到我一开始的状态。下面的XSLT不会失败,但也不会做任何转换。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="no"/>
    <xsl:template match="/">
        <xsl:copy-of select="*"/>
    </xsl:template>
</xsl:stylesheet>

我应该在此XSLT中添加什么来替换源XML中的<p><br>标记?

3 个答案:

答案 0 :(得分:1)

我认为您的意思是要删除物理P / BR标记但保留其内容。

在这种情况下,请参阅此XMLPlayground会话(请参阅输出源中的XML)

http://www.xmlplayground.com/9OE0NI

迭代模板执行以下两项操作之一:

  • 如果当前节点是P / BR,则仅输出其内容,而不是标签
  • 否则,输出标签和内容

...然后递归子节点。

答案 1 :(得分:0)

每个元素都需要一个模板:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
    <xsl:template match="p">
        <xsl:copy-of select="*" />
    </xsl:template>
    <xsl:template match="br">
        <!-- -->
    </xsl:template>
</xsl:stylesheet>

答案 2 :(得分:0)

对于那些感兴趣的人,这是我用来替换<br><p>标签的XSL。你需要xmlns:fo =“http://www.w3.org/1999/XSL/Format你想输出像<fo:block>

这样的XSLFO
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output omit-xml-declaration="yes" indent="yes" />

<xsl:template match="/">
    <xsl:apply-templates select='*' />
</xsl:template>

<xsl:template match='*'>
    <xsl:choose>
        <xsl:when test='name() = "p"'>
            <fo:block>
              <xsl:value-of select='.' />
            </fo:block>
        </xsl:when>
        <xsl:when test='name() = "br"'>
             <fo:block></fo:block>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy select='.' />
        </xsl:otherwise>
    </xsl:choose>
    <xsl:apply-templates select='*' />
</xsl:template>

相关问题