javascript - XSLT:从 XML 节点中的代码片段构建 HTML 文件

标签 javascript xslt

我想知道是否有人可以帮助我解决我遇到的棘手问题。可能会在这里提出不可能的问题,但有兴趣听取任何建议。

我有一个 XML 文件,其中一个节点包含页面的 HTML。我想获取该节点中的数据以及如何从中创建 HTML 文件。因此,从下面的示例 XML 中,我想从 //dvm/rows/row 转换并选择 cell[2] 节点并将其放置在另一个文件中,以便我实际上可以看到 HTML 标记显示的内容,并将其显示为正确的网页而不是代码。

如果我像这样把它和 XSL 放在一起,它会起作用吗?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/">
    <xsl:value-of select="//dvm/rows/row/cell[2]" disable-output-escaping="yes"/>   
</xsl:template> 
</xsl:stylesheet>

XML 是:

<dvm>
  <description>This is a DVM file</description>
  <columns>
    <column name="lang"/>
    <column name="text"/>
    <column name="code" qualifier="true" order="1"/>
    <column name="type" qualifier="true" order="2"/>
    <column name="subj"/>
    <column name="urem"/>
    <column name="vreq"/>
 </columns>
 <rows>
   <row>
     <cell>English(UK)</cell>
     <cell><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
           <html xmlns="http://www.w3.org/1999/xhtml">
           <head>
              <title>My HTML File</title>
           </head>
           <body>
           <div class="text">
             <p>This is a line of text</p>
             <p>This is a line of text</p>
             <p>This is a line of text</p>  
           </div>
           </body>
           </html>]]>
     </cell>
     <cell>Code 1</cell>
     <cell>Text</cell>
     <cell>HTML Block 1</cell>
     <cell/>
     <cell/>
   </row>
 </rows>
</dvm>

最佳答案

您必须删除 CDATA 部分并使用它包含的文本作为常规标记。 DOCTYPE 也应该被删除(它将由转换生成):

<?xml-stylesheet type="text/xsl" href="delete_3.xsl" ?>
<dvm>
    <description>This is a DVM file</description>
    <columns>
        <column name="lang"/>
        <column name="text"/>
        <column name="code" qualifier="true" order="1"/>
        <column name="type" qualifier="true" order="2"/>
        <column name="subj"/>
        <column name="urem"/>
        <column name="vreq"/>
    </columns>
    <rows>
        <row>
            <cell>English(UK)</cell>
            <cell>
               <html xmlns="http://www.w3.org/1999/xhtml">
               <head>
                  <title>My HTML File</title>
               </head>
               <body>
               <div class="text">
                 <p>This is a line of text</p>
                 <p>This is a line of text</p>
                 <p>This is a line of text</p>
               </div>
               </body>
               </html>
      </cell>
            <cell>Code 1</cell>
            <cell>Text</cell>
            <cell>HTML Block 1</cell>
            <cell/>
            <cell/>
        </row>
    </rows>
</dvm>

那么生成包含在第二个 cell 元素中的 XHTML 的转换很简单:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select="/*/rows/*/cell[2]/node()"/>
 </xsl:template>
</xsl:stylesheet>

当我现在用IE打开文件:c:\temp\delete\delete2.xml时,结果是:

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title>My HTML File</title>
   </head>
   <body>
      <div class="text">
         <p>This is a line of text</p>
         <p>This is a line of text</p>
         <p>This is a line of text</p>
      </div>
   </body>
</html>

浏览器显示为:

我的 HTML 文件

这是一行文字

这是一行文字

这是一行文字

关于javascript - XSLT:从 XML 节点中的代码片段构建 HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10429394/

相关文章:

javascript - 将 JSON 对象列表从 javascript 发送到 MVC 操作

xslt - 如何在 xslt 元素上应用分组依据

xml - 将多个元素值连接到 xslt 2.0 中的新字符串的最佳方法是什么

xml - 在 xslt 中更改 namespace uri

javascript - 链接到 native Javascript 函数

JavaScript 缺少分号?

javascript - 使用 jQuery 拆分表单字段值

javascript - javascript 中的变量作用域

html - XML/XSL 表不显示

xslt - 如何在visual studio环境中使用EXSLT