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

Java 如何对html字符串进行xsl模板匹配,java,html,xml,xslt,xsl-fo,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> pbuHeelo/u/b/p 我需要在pdf中呈现这个 <xsl:template match="b"&

我有一个场景,需要使用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>

pbuHeelo/u/b/p
我需要在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>的方法


提前谢谢你的帮助

如果您想解析HTML,您需要一种集成HTML解析器的方法,如果您在XSLT 2中使用David Carlisle的HTML解析器实现,则可以使用XSLT 2处理器,然后您可以导入它并调用函数将
元素的内容解析到要由模板处理的节点中:

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

如果您想解析HTML,您需要一种集成HTML解析器的方法,如果您在XSLT 2中使用David Carlisle的HTML解析器实现,则可以从中导入HTML解析器,然后调用函数将
部分
元素的内容解析到要由模板处理的节点中:

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

你能使用XSLT 3.0吗?你能使用XSLT 3.0吗?嗨,Martin,我在使用XSLT 1.0,其中包含xalan,那么有没有支持XSLT 1.0的解析器?Java很容易从xalan和XSLT 1切换到Saxon 9(Saxon 9他分别是开源的)和XSLT 2/3。我怀疑是否有人在XSLT 1中使用过HTML解析器,但当然也可以使用Xalan在您的工作流程中插入任何Java实现的HTML解析器,请参见。嗨,Martin,我使用的XSLT 1.0中包含Xalan,那么是否有任何解析器支持XSLT 1.0和Java?很容易从Xalan和XSLT 1切换到Saxon 9(Saxon 9,他分别是开源的)和XSLT 2/3。我怀疑是否有人在XSLT 1中做过HTML解析器,但当然也可以使用Xalan将任何用Java实现的HTML解析器插入到您的工作流中,请参阅。