使用XSLT和Print to HTML解析CData中的XML元素

时间:2013-11-27 17:27:24

标签: xslt

输入xml is

<getArtifactContentResponse>
<return>
<![CDATA[
<metadata>
<overview>       
<name>scannapp</name>
<developerId>developer702</developerId>
<stateId>2</stateId>
<serverURL>abc.com</serverURL>
<id>cspapp1103</id>
<description>scann doc</description>
<hostingTypeId>1</hostingTypeId>       
</overview>
</metadata>
  ]]>
  </return>
</getArtifactContentResponse>

下面是我开发的样式表。我能够在Cdata中检索XML但无法获取元素值。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="html" encoding="utf-8" omit-xml-declaration="no" indent="no"/>
   <xsl:template match="/">
      <html>
         <body>
            <h1>Company Details</h1>
            <table border="1">
               <tr>
                  <th>name</th>
                  <th>developerId</th>
                  <th>Id</th>
               </tr>table

               <xsl:variable name ="data" select="//getArtifactContentResponse/return/node()" />
                  <tr>
                     <td>
                        <xsl:value-of select="$data/metadata/overview/name" disable-output-escaping="yes"/>


                     </td>
                     <td>
                        <xsl:value-of select="$data/metadata/overview/developerId" />
                     </td>
                     <td>
                        <xsl:value-of select="$data/metadata/overview/Id" />
                     </td>
                  </tr>

            </table>

         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

输出为

<html><body><h1>Company Details</h1><table border="1"><tr><th>name</th><th>developerId</th><th>serverURL</th></tr>table


                  <tr><td></td><td></td><td></td></tr></table></body></html>

预期输出

<html><body><h1>Company Details</h1><table border="1"><tr><th>name</th><th>developerId</th><th>serverURL</th></tr>table


                  <tr><td>scannapp</td><td>developer702</td><td>cspapp1103</td></tr></table></body></html>

我想取值名称,developerId,Id并打印到HtML。怎么做请帮助我。使用XSLT version1.0。

1 个答案:

答案 0 :(得分:1)

AFAIK,无法将CDATA 解析为XML

使用扩展功能将解析为部分会很好,但不是必需的。这是一个提取你所追求的三个项目的例子:

...
<xsl:variable name ="cdata" select="/getArtifactContentResponse/return" />

<name>
    <xsl:value-of select="substring-before(substring-after($cdata, '&lt;name&gt;'), '&lt;/name&gt;')"/>
</name>

<developerId>
    <xsl:value-of select="substring-before(substring-after($cdata, '&lt;developerId&gt;'), '&lt;/developerId&gt;')"/>
</developerId>

<id>
    <xsl:value-of select="substring-before(substring-after($cdata, '&lt;id&gt;'), '&lt;/id&gt;')"/>
</id>
...