2012-06-08 38 views
0

我正在使用XSL-FO和FOP来生成PDF。我正在将复杂的HTML页面转换为PDF。执行XML/XSLT转换需要一些帮助

我遇到了以下错误:

未知格式化对象 “{} BR” 遭遇(p的孩子)。 (没有上下文可用)

FOP处理器不理解我提供的XSL-FO的格式,因为它里面还有一些HTML标签。我想过滤的XML <p><br/>标签链接如下:

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

在最后一位,在“标签要素1”和“Tab元素2”,你可以看到<p><br/>是FOP不明白。

你能帮我用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>标签?

回答

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

我假定你的意思是你要删除的物理P/BR标签,但保留其内容。

在这种情况下,请参阅本XMLPlayground会议(见在输出源XML)

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

迭代模板并两件事情之一:如果当前节点是P

  • /BR,只输出其内容,而不是标签
  • 否则,输出标签和内容

...然后递归子节点。

+0

感谢比如我解决了这个问题。通过将xsl-fo命名空间添加到xslt文件并将示例更改为我的需要 – Julius

0

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

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

+0

嗯,我认为这有效,但实际上在应用此XSLT时缺少很多源XML。接受xsl:copy元素上的select属性... – Julius