java - 如何对 html 字符串进行 xsl 模板匹配

标签 java html xml xslt xsl-fo

我有一个场景,我需要使用 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时如何实现这一点或者有什么方法可以将 < 替换为 < 和 > 为 > ?

提前感谢您的帮助!!!

最佳答案

如果您想解析 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

我已经使用了您的问题中所示的模板,但请注意,您可以简化 <xsl:apply-templates select="*|text()" /> 的所有用途。至<xsl:apply-templates/>通常情况下。

其他方式取决于所使用的特定 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

关于java - 如何对 html 字符串进行 xsl 模板匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978233/

相关文章:

java - 在 Java 中抛出自定义异常与内置异常

java - bundle 的 Java 应用程序被识别为 32 位,它可以在 macOS 10.15 Catalina 下运行吗?

java - 方法完成后程序继续运行

java - 从下到上创建类实例的 XML 表示?

java - 如何更改 Firestore 中特定事件按钮的文本和颜色?

Java ImageView获取位置并设置对齐方式

javascript - 无法调用未定义的方法 'find'

java - 如何从 Java 编辑 xsl 标签属性文件

javascript - 在 jQuery 中使用 find 方法获取多个标签值

javascript - 在一页内平滑滚动