【问题标题】:How to do a xsl-template match for a html string如何对 html 字符串进行 xsl 模板匹配
【发布时间】:2018-12-30 22:33:35
【问题描述】:

我有一个场景,我需要使用 XSLT 在 pdf 中呈现 html。我在 xml 文件中有一些 html 内容,例如

<section>
&lt;p&gt;&lt;b&gt;&lt;u&gt;Heelo&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
</section>

我需要在 pdf 中呈现这个。

 <xsl:template match="b">
    <fo:inline font-weight="bold">
        <xsl:apply-templates select="*|text()" />
    </fo:inline>
</xsl:template>

<xsl:template match="u">
    <fo:inline text-decoration="underline">
        <xsl:apply-templates select="*|text()" />
    </fo:inline>
</xsl:template>

<xsl:template match="i">
    <fo:inline font-style="italic">
        <xsl:apply-templates select="*|text()" />
    </fo:inline>
</xsl:template>

但是这个模板匹配不起作用。在java中创建xml时如何实现这一点或有什么方法可以替换 as >?

提前感谢您的帮助!!!

【问题讨论】:

  • 你能使用 XSLT 3.0 吗?

标签: java html xml xslt xsl-fo


【解决方案1】:

如果您想解析 HTML,您需要一种集成 HTML 解析器的方法,如果您使用来自 https://github.com/davidcarlisle/web-xslt/blob/master/htmlparse/htmlparse.xsl 的 XSLT 2 中 David Carlisle 的 HTML 解析器实现,则可以通过 XSLT 2 处理器实现,然后您可以导入它并调用将section 元素的内容解析为要由模板处理的节点的函数:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:d="data:,dpc"
    exclude-result-prefixes="#all"
    version="3.0">

<xsl:import href="https://raw.githubusercontent.com/davidcarlisle/web-xslt/master/htmlparse/htmlparse.xsl"/>

<xsl:output indent="yes"/>

<xsl:template match="/">
  <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="first" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
          <fo:region-body margin-top="1cm"/>
          <fo:region-before extent="1cm"/>
          <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>


      <fo:page-sequence master-reference="first">
         <fo:flow flow-name="xsl-region-body">  
           <fo:block>
               <xsl:apply-templates/>
           </fo:block>
         </fo:flow>
      </fo:page-sequence>
  </fo:root>
</xsl:template>

<xsl:template match="section">
    <fo:block>
        <xsl:apply-templates select="d:htmlparse(., '', true())/node()"/>
    </fo:block>
</xsl:template>

<xsl:template match="b">
    <fo:inline font-weight="bold">
        <xsl:apply-templates select="*|text()" />
    </fo:inline>
</xsl:template>

<xsl:template match="u">
    <fo:inline text-decoration="underline">
        <xsl:apply-templates select="*|text()" />
    </fo:inline>
</xsl:template>

<xsl:template match="i">
    <fo:inline font-style="italic">
        <xsl:apply-templates select="*|text()" />
    </fo:inline>
</xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/94hvTAp

我已经使用了您的模板,如您的问题中所示,但请注意,您可以将&lt;xsl:apply-templates select="*|text()" /&gt; 的所有用法正常简化为&lt;xsl:apply-templates/&gt;。

其他方式取决于所使用的特定 XSLT 处理器(即它是否提供像 http://saxonica.com/html/documentation/functions/saxon/parse-html.html 这样的扩展,或者它是否允许您实现自己的集成 HTML 解析器的扩展功能)。

如果 HTML 是格式良好的 XML(例如,具有所有必要的结束标记和引号属性,不使用 HTML 特定的实体引用),那么您还可以将 XPath 3.1 函数 parse-xml-fragment 与 XSLT 3 处理器(如 Saxon)一起使用9.8 或更高版本:

<xsl:template match="section">
    <fo:block>
        <xsl:apply-templates select="parse-xml-fragment(.)/node()"/>
    </fo:block>
</xsl:template>

https://xsltfiddle.liberty-development.net/94hvTAp/1

【讨论】:

猜你喜欢
  • 2019-02-11
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
相关资源
最近更新 更多