【问题标题】:XML with HTML and XSLT transformation带有 HTML 和 XSLT 转换的 XML
【发布时间】:2012-03-19 06:08:06
【问题描述】:

我尝试修改一个在标签内包含 html 的 xml 文档,我可以转换 <p> 并列出但我里面有粗体和斜体。我怎样才能做到这一点?我使用 xslt 模板进行转换。

这是我的代码:

<Memoria>     
  <titulo>Memoria EUITI 2010</titulo>
  <Indice>
     <titulo>Indice 2010</titulo>
    <secciones>
      <seccion>
       <parent_id>1</parent_id>
         <titulo>INFORMACI&#211;N GENERAL</titulo>
          <contenido><p><strong>ESTO</strong> ES UNA <em>PRUEBA</em></p></contenido>
<ul>
    <li> caso de poner</li>
</ul>
<p>Ver si funciiona esto que si funciona ya esta</p>
<p> </p></contenido
      </seccion>
    </secciones>
  </Indice>
</Memoria>

我使用这个 xslt 转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/"> 

     <Memoria>
       <xsl:apply-templates select="//Memoria"/>
     </Memoria>
  </xsl:template>
  <xsl:template match="text()"/>

  <xsl:template match="titulo">
    <titulo>
      <xsl:value-of select="."/>
    </titulo>
  </xsl:template>

  <xsl:template match="Indice/titulo">
   <Indice>
      <xsl:value-of select="."/>
    </Indice>
  </xsl:template>

  <xsl:template match="Indice/secciones/seccion">
     <Seccion>
      <xsl:apply-templates select="contenido|titulo"/>
   </Seccion>

  </xsl:template>

  <xsl:template match="Indice/secciones/seccion/titulo">
    <nombre_seccion>
      <xsl:value-of select="."/>
    </nombre_seccion>
  </xsl:template>  


  <xsl:template match="contenido">
    <Contenido>
      <xsl:apply-templates select="p|ul"/>
    </Contenido>
  </xsl:template>


  <xsl:template match="p">
    <p>
      <xsl:value-of select="."/>
    </p>
  </xsl:template>

  <xsl:template match="ul">
    <ul>
      <xsl:for-each select="li">
    <li>
      <xsl:value-of select="."/>
    </li>
      </xsl:for-each>
    </ul>
 </xsl:template>
</xsl:stylesheet>

使用此模板,我得到了一个 xml 有效文档,但该文档没有进入:

  <contenido><p><strong>ESTO</strong> ES UNA <em>PRUEBA</em></p></contenido>

我该怎么做?

另一方面,我想用 XSL-FO 中的另一个 XSLT 转换这个结果文档,以生成带有 FOP 的 PDF。我可以生成 p 和一个列表,但我不能生成粗体和草书。

这是正确的做法吗:

HTML->XML->XSL-FO

或者有什么方法可以直接转换代码?

【问题讨论】:

  • 正确缩进代码.. 使用常规名称,如root, foo, bar, child。让您的问题更简单、更整洁.. 尝试将您的工作 XML 文档简化为示例 XML,以便它易于阅读和理解。像这样的问题听起来并不有趣..

标签: html xml xslt apache-fop


【解决方案1】:

如果你想复制一个元素的混合内容,即包括所有标记,那么你需要使用copy-of而不是value-of。例如:

<xsl:template match="p">
  <xsl:copy-of select="."/>
</xsl:template>

这会将 p 元素本身、所有文本内容和所有子元素(包括 等)放入输出中。

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2012-09-29
    • 2012-06-02
    • 2023-03-26
    相关资源
    最近更新 更多