xsl格式化xml后能输出为XML格式吗?不要输出html格式
我想用xsl实现模糊查找功能,实现方法是用xsl进行过滤。
但是过滤后输出是html形式的,我想要过滤后还是xml格式,跟过滤之前的xml格式一样的,只显示我要的xml数据。
比如:我的xml格式是这样的:
<?xml version= "1.0 " encoding= "gb2312 "?>
<company>
<department name= "Research & Development ">
<employee name= 'JohnDoe ' job= 'Software Analyst ' salary= '2000 '/>
<employee name= 'Jane Fletcher ' job= 'Designer ' salary= '2500 '/>
<employee name= 'Chris Benton ' job= 'Programmer ' salary= '3100 '/>
</department>
</company>
-------我只想取name属性中包含a的那一行,我想用xsl过滤后数据是下面的格式,而且是xml格式,可以用xmlDom.loadXml()加载的,而不是以html的格式显示在网页上。
<?xml version= "1.0 " encoding= "gb2312 "?>
<company>
<department name= "Research & Development ">
<employee name= 'Jane Fletcher ' job= 'Designer ' salary= '2500 '/>
</department>
</company>
请帮忙想想办法。谢谢
附上代码:
-------test.xml
<?xml version= "1.0 " encoding= "iso-8859-1 "?>
<?xml-stylesheet type= "text/xsl " href= "test.xsl "?>
<company>
<department name= "Research & Development ">
<employee name= 'JohnDoe ' job= 'Software Analyst ' salary= '2000 '/>
<employee name= 'Jane Fletcher ' job= 'Designer ' salary= '2500 '/>
<employee name= 'Chris Benton ' job= 'Programmer ' salary= '3100 '/>
</department>
</company>
-------------test.xsl
<?xml version= "1.0 " encoding= "gb2312 "?>
<xsl:stylesheet version= "1.0 " xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:template match= "/ ">
<company>
<br />
<xsl:for-each select= "company/department/employee ">
<xsl:if test= "contains(@name, 'a ') ">
<employee name= ' <xsl:value-of select= "@name "/> ' job= ' <xsl:value-of select= "@job "/> ' salary= ' <xsl:value-of select= "@salary "/> '/>
<br />
</xsl:if>
</xsl:for-each>
<company/>
</xsl:template>
</xsl:stylesheet>
---结果是以html格式显示的,不是XML格式:
<company>
<employee name= 'Jane Fletcher ' job= 'Designer ' salary= '2500 '/>
<employee name= 'Jane Fletcher ' job= 'Designer ' salary= '2500 '/>
<company/>
[解决办法]
<?xml version= "1.0 "?>
<xsl:stylesheet version= "1.0 " xmlns:xsl= "http://www.w3.org/1999/XSL/Transform ">
<xsl:output indent= "yes " method= "xml " standalone= "yes "/>
<xsl:param name= "depName "> Research & Development </xsl:param>
<xsl:param name= "key "> a </xsl:param>
<xsl:template match= "/company/department ">
<company>
<xsl:choose>
<xsl:when test= "@name=$depName ">
<department name= "{$depName} ">
<xsl:copy-of select= "employee[contains(@name,$key)] " />
</department>
</xsl:when>
</xsl:choose>
</company>
</xsl:template>
</xsl:stylesheet>