生成Xml,不包括>和<使用xslt

时间:2011-11-10 06:11:37

标签: xml xslt escaping

我需要删除Root节点并在XML格式的网页上显示xslt。

为了更清楚,我将直接提供我想要的输出XML文件和我给的输入XMl。

我的输入XML文件是: -

<cp:gtl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cp="urn:schemas-microsoft-com/contentpublishing/content" xmlns:ns1="http://www.w3.org/1999/xhtml" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-03-30T18:40:19" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">
<cp:abstract>
&lt;weatherdata&gt;&lt;weather entityid="32226" alert="" timezone="5.5" long="72.8195343" lat="21.195015" attribution2="© Foreca" attribution="Data provided by Foreca" provider="Foreca" degreetype="F" imagerelativeurl="http://stjp.msn.com/as/wea3/i/en-us/" url="http://local.msn.com/worldweather.aspx?eid=32226&amp;q=Surat-IND" encodedlocationname="Surat%2c+IND" zipcode="" weatherlocationname="Surat, IND" weatherlocationcode="wc:INXX0157"&gt;&lt;current winddisplay="4 mph NE" windspeed="4" humidity="38" feelslike="90" observationpoint="Surat" observationtime="11:30:00" shortday="Wed" day="Wednesday" date="2011-11-09" skytext="Clear" skycode="32" temperature="90"/&gt;&lt;forecast shortday="Wed" day="Wednesday" date="2011-11-09" precip="5" skytextday="Clear" skycodeday="32" high="95" low="72"/&gt;&lt;forecast shortday="Thu" day="Thursday" date="2011-11-10" precip="5" skytextday="Clear" skycodeday="32" high="96" low="72"/&gt;&lt;forecast shortday="Fri" day="Friday" date="2011-11-11" precip="5" skytextday="Clear" skycodeday="32" high="96" low="72"/&gt;&lt;forecast shortday="Sat" day="Saturday" date="2011-11-12" precip="5" skytextday="Clear" skycodeday="32" high="95" low="75"/&gt;&lt;forecast shortday="Sun" day="Sunday" date="2011-11-13" precip="5" skytextday="Fair" skycodeday="34" high="96" low="79"/&gt;&lt;toolbar minversion="1.0.1965.0" timewindow="60"/&gt;&lt;/weather&gt;
&lt;/weatherdata&gt;
</cp:abstract>
</cp:gtl>

我在网页上的预期输出文件是

<?xml version="1.0"?>
<weatherdata>
<weather entityid="32226" alert="" timezone="5.5" long="72.8195343" lat="21.195015" attribution2="© Foreca" attribution="Data provided by Foreca" provider="Foreca" degreetype="F"   encodedlocationname="Surat%2c+IND" zipcode="" weatherlocationname="Surat, IND" weatherlocationcode="wc:INXX0157"><current winddisplay="4 mph NE" windspeed="4" humidity="38" feelslike="90" observationpoint="Surat" observationtime="11:30:00" shortday="Wed" day="Wednesday" date="2011-11-09" skytext="Clear" skycode="32" temperature="90"/><forecast shortday="Wed" day="Wednesday" date="2011-11-09" precip="5" skytextday="Clear" skycodeday="32" high="95" low="72"/><forecast shortday="Thu" day="Thursday" date="2011-11-10" precip="5" skytextday="Clear" skycodeday="32" high="96" low="72"/><forecast shortday="Fri" day="Friday" date="2011-11-11" precip="5" skytextday="Clear" skycodeday="32" high="96" low="72"/><forecast shortday="Sat" day="Saturday" date="2011-11-12" precip="5" skytextday="Clear" skycodeday="32" high="95" low="75"/><forecast shortday="Sun" day="Sunday" date="2011-11-13" precip="5" skytextday="Fair" skycodeday="34" high="96" low="79"/><toolbar minversion="1.0.1965.0" timewindow="60"/></weather>
</weatherdata>

然而目前正在发生的事情是我能够删除cp:abstract标签并且我的XML在网页上可见正确,但是当我查看页面源时,我仍然看到&lt;和&gt;由&gt;取代和&lt;

我当前写的xslt是: -

<xslt:stylesheet version="1.0" exclude-result-prefixes="xslt cp gc"     xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:cp="urn:schemas-microsoft-com/contentpublishing/content" xmlns:gc="http://schemas.microsoft.com/msn/granite/cm"
             xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
<xslt:output omit-xml-declaration="yes" method="xml"/>

<xslt:template match="gc:cm/gc:content/cp:root/cp:gtl">
    <xslt:apply-templates select="cp:abstract"/>
</xslt:template>

<xslt:template match="cp:abstract">
    <xslt:value-of select="." />
</xslt:template>

有人可以帮我纠正我的xslt,以便获得所需的XML输出吗? 另请注意,cp:abstract标签下的XML是从feed中随机生成的。有时它是weathaer,有时是一些其他动态xml。我想要的只是cp:abstract标签下的根节点,以XML格式在我的网页上提取。

1 个答案:

答案 0 :(得分:1)

我认为您所说的是当您在浏览器中查看结果输出时,您会看到类似这样的内容......

<weatherdata> 
   <weather entityid="32226" alert="" ....
</weatherdata>

但是当你做“查看来源”时,你会看到这个

&lt;weatherdata&gt;
    &lt;weather entityid="32226" alert=""
&lt;/weatherdata&gt; 

也就是说,它已被'转义'用于显示目的。问题是您的 cp:abstract 标记包含一串文本,而不是XML字符串,因此您正在输出XML,XSLT处理器将对文本进行转义,以免影响主XML文档。

解决方法就是在 xsl:value-of 元素上使用 disable-output-escaping

<xslt:template match="cp:abstract"> 
   <xslt:value-of select="." disable-output-escaping="yes"  /> 
</xslt:template> 

完成后,将输出以下文本,恰好是XML格式

<weatherdata> 
   <weather entityid="32226" alert="" ....
</weatherdata>

或者,您可以将 xsl:output 语句中的方法属性更改为文本,这应该具有相同的效果

<xslt:output omit-xml-declaration="yes" method="text"/>