使用XSLT输出转换的XML

时间:2015-09-23 19:56:28

标签: xml xslt

我想在XML上使用XSLT执行一些转换,并将结果XML输出到textarea。

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

输出源XML。

有没有办法改变它?

1 个答案:

答案 0 :(得分:0)

您可以将未编码的xml转储到textarea中,它将如此呈现。例如,这个XSLT 2.0样式表...

<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
<xsl:output method="html" version="5" encoding="UTF-8" indent="yes" />

<xsl:template match="/">
  <hmtl>
    <head><title>XML Document Listing</title></head>
    <body>
      <h1>Document Listing</h1>
      <textarea cols="60" rows="10">
        <xsl:apply-templates />
      </textarea>
    </body>  
  </hmtl>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>

</xsl:transform>

...当应用于此输入文档时......

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>    
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>    
</catalog>

...将产生结果......

&#13;
&#13;
<!DOCTYPE HTML>
<hmtl>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>XML Document Listing</title>
   </head>
   <body>
      <h1>Document Listing</h1><textarea cols="60" rows="10"><catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog></textarea></body>
</hmtl>
&#13;
&#13;
&#13;