【问题标题】:XSLT inject HTMLXSLT 注入 HTML
【发布时间】:2011-09-09 09:24:11
【问题描述】:

我有以下 XML:

<person>
      <name>John</name>
      <htmlDescription>A <strong>very</strong> <b><i>nice</i></b> person </htmlDescription>
</person>

我想以某种方式在 XSLT 转换中使用这个 XML 来生成 HTML,例如

一个非常 很好

这可以使用 XSLT 吗?

【问题讨论】:

    标签: html xml xslt xhtml


    【解决方案1】:

    如果将其转换为 HTML:

    <xsl:stylesheet version='1.0'
                    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    
    <xsl:output method='html'/>
    
    <xsl:template match='person'>
        <td>
            <xsl:copy-of select='htmlDescription/node()'/>
        </td>
    </xsl:template>
    
    </xsl:stylesheet>
    

    如果将其转换为 XHTML:

    <xsl:stylesheet version='1.0'
                    xmlns='http://www.w3.org/1999/xhtml'
                    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    
    <xsl:output method='xml'/>
    
    <xsl:template match='htmlDescription//text()'>
        <xsl:value-of select='.'/>
    </xsl:template>
    
    <xsl:template match='htmlDescription//*'>
        <xsl:element name='{local-name()}'>
            <xsl:copy-of select='@*'/>
            <xsl:apply-templates select='node()'/>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match='person'>
        <td>
            <xsl:apply-templates select='htmlDescription/node()'/>
        </td>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      是的,这是可能的。这个例子对我有用:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns:fn="http://www.w3.org/2005/xpath-functions">
      <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
      
      <xsl:template match="person" >
          <xsl:element name="td">
              <xsl:copy-of select="htmlDescription/*"  />
          </xsl:element>
      </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

      • 完美的“复制”是关键。非常感谢!
      • @VoodooRider: htmlDescription/* 仅匹配 &lt;htmlDescription&gt; 内的元素,但不匹配文本节点(即 A 和 `person` 不会被复制)。看我的回答。
      【解决方案3】:
      <xsl:template match="person">
          <xsl:element name="td">
              <xsl:value-of select="htmlDescription"/>
          </xsl:element>   
      </xsl:template>
      

      【讨论】:

      • 问题是 xlst 转换器转义了一些字符,例如“ A very nice人 插入了一个 非常 nice
      • 输出是正确的 HTML,也许您的网络浏览器不会将页面解释为 HTML,而是解释为 XML。尝试将&lt;xsl:output method="html" encoding="ISO-8859-1" indent="yes"/&gt; 添加到您的XSLT 脚本中(几乎在开始时)。如果没有,可能还有其他事情要做,具体取决于您的 XSLT Transformer 及其使用的环境。
      【解决方案4】:
      <xsl:template match="person">
          <tr>
              <td>
                  <xsl:value-of select="name"/>
              </td>
               <td>
                  <xsl:value-of select="htmlDescription"/>
              </td> 
          </tr>  
      </xsl:template>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-03
        • 2014-06-06
        • 2020-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多