简体   繁体   English

使用xslt生成不包含>和<的Xml

[英]Generate Xml excluding the > and < using xslt

I have a requirement where in i need to remove the Root node and display the xslt on a webpage in XML format. 我有一个需要删除根节点并以XML格式在网页上显示xslt的要求。

To make it more clear, I will directly give my desired output XML file and the Input XMl that I am giving. 为了更加清楚,我将直接给出所需的输出XML文件和给出的Input XMl。

My Input XML file is: 我的输入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>

And my expected output file on Webpage is: 我在网页上的预期输出文件是:

<?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>

However currently what is happening is I am able to remove the cp:abstract tag and my XML is visible correct on the web page, however when I View the page source, I still see < and > replaced by > and < 但是当前正在发生的事情是我能够删除cp:abstract标记,并且我的XML在网页上可见,但是,当我查看页面源代码时,仍然看到<和>替换为>和<

My current xslt which I have written is: 我当前编写的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>

Can some one please help me correct my xslt so that I get the desired XML output? 有人可以帮我纠正我的xslt,以便获得所需的XML输出吗? Also note that XML under cp:abstract tag is random generated from feed. 另请注意,cp:abstract标记下的XML是从feed随机生成的。 Sometimes it is fo weathaer, sometimes some other dynamic xml. 有时是因为麻烦,有时是其他一些动态xml。 All I want is root node under cp:abstract tag to be extracted on my webpage in XML format. 我想要的只是cp:abstract标记下的根节点以XML格式提取到我的网页上。

I think what you are saying is when you view the resultant output in a browser, you see something like this... 我认为您的意思是,当您在浏览器中查看结果输出时,会看到类似这样的内容...

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

But when you do a 'View Source', you see this 但是当您执行“查看源代码”时,您会看到

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

That is to say, it has been 'escaped' for display purposes. 也就是说,出于显示目的已对其进行了“转义”。 The issue is your cp:abstract tag contains a string of text, not a string of XML, and so because you are outputing XML, the XSLT processor will escape the text it so as not to potential affect the main XML document. 问题是您的cp:abstract标记包含一个文本字符串,而不是XML字符串,因此,由于您正在输出XML,因此XSLT处理器将转义该文本,以免潜在影响主XML文档。

The solution is to do this is do use disable-output-escaping on the xsl:value-of element 解决方案是这样做是在xsl:value-of元素上使用disable-output-ecaping

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

When this is done, the following text will be output, which just happens to be in XML format 完成此操作后,将输出以下文本,该文本恰好是XML格式

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

Alternatively, you could change the method attribute on the xsl:output statement to text , and that should have the same effect 或者,您可以将xsl:output语句上的method属性更改为text ,并且应该具有相同的效果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM