當xslt中的任何一個爲空時,我的xslt會返回已關閉的標記 - (類別和描述標記)。在下面的示例中,標題返回已關閉描述標記,而標題2將返回已關閉的類別標記。XML空節點,XSLT返回已關閉標記
我正在將我的源代碼添加到應用程序中,並且因爲沒有打開的封閉標記,我沒有得到預期的結果。
這是我輸入:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Title</title>
<description>Description</description>
<link>https://url/</link>
<language>en-us</language>
<lastBuildDate>Sun, 27 Sep 2015 21:07:40 GMT</lastBuildDate>
<image>
<title>Title</title>
<width>144</width><height>400</height>
<link>https://url</link>
<url>https://url</url>
</image>
<item>
<title>Title</title>
<link>https://html</link>
<guid isPermaLink="true">https://html</guid>
<comments>https://html</comments>
<pubDate>Thu, 24 Sep 2015 16:01:42 GMT</pubDate>
<description><![CDATA[]]></description>
<category>headline,government,usa,spyware</category>
</item>
<item>
<title>Title2</title>
<link>https://txt</link>
<guid isPermaLink="true">https://txt</guid>
<comments>https://html</comments>
<pubDate>Fri, 25 Sep 2015 06:54:51 GMT</pubDate>
<description>DescritpionExist</description>
<category></category>
</item>
</channel>
</rss>
以下是我的XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--skips new lines and indents in-->
<xsl:output method="xml" indent="yes"/>
<!--if,error-->
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!--skips completely-->
<xsl:template match="channel/title"/>
<xsl:template match="channel/description"/>
<xsl:template match="channel/link"/>
<xsl:template match="channel/language"/>
<xsl:template match="channel/lastBuildDate"/>
<xsl:template match="image"/>
<xsl:template match="item/link"/>
<!--skips node if empty-->
<xsl:template match="item/title[not(child::node())]"/>
<xsl:template match="item/link[not(child::node())]"/>
<xsl:template match="item/guid[not(child::node())]"/>
<xsl:template match="item/comments[not(child::node())]"/>
<!--delimits values if seperated by comma-->
<xsl:template match="item/category[contains(.,',')]">
<xsl:variable name="elementName" select="name(..)"/>
<xsl:call-template name="splitIntoElements">
<xsl:with-param name="baseName" select="name(..)" />
<xsl:with-param name="txt" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="splitIntoElements">
<xsl:param name="baseName" />
<xsl:param name="txt" />
<xsl:param name="delimiter" select="','" />
<xsl:param name="index" select="1" />
<xsl:variable name="first" select="substring-before($txt, $delimiter)" />
<xsl:variable name="remaining" select="substring-after($txt, $delimiter)" />
<xsl:element name="{$baseName}-{$index}">
<xsl:choose>
<xsl:when test="$first">
<xsl:value-of select="$first" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$txt" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:if test="$remaining">
<xsl:call-template name="splitIntoElements">
<xsl:with-param name="baseName" select="$baseName" />
<xsl:with-param name="txt" select="$remaining" />
<xsl:with-param name="index" select="$index+1" />
<xsl:with-param name="delimiter" select="$delimiter" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
跳躍或刪除空字段沒有幫助:
<xsl:template match="item/category[not(child::node())]"/>
<xsl:template match="item/description[not(child::node())]"/>
也不幫
<xsl:template match="*[normalize-space() = '']" />
我甚至試圖用盡,如果再子句的結束標記前添加一個開始標記,但我沒能得到結果。
我的輸出是:
<item>
<title>Title</title>
<guid isPermaLink="true">http</guid>
<comments>http</comments>
<pubDate>Thu, 24 Sep 2015 01:54:35 GMT</pubDate>
<description/>
<item-1>headline</item-1>
<item-2>government</item-2>
<item-3>privacy</item-3>
<item-4>usa</item-4>
</item>
<item>
<title>Title2</title>
<guid isPermaLink="true">http</guid>
<comments>comment</comments>
<pubDate>Sat, 26 Sep 2015 03:14:57 GMT</pubDate>
<description>Description</description>
<category/>
</item>
預期的輸出應該是:
<item>
<title>Title</title>
<guid isPermaLink="true">http</guid>
<comments>http</comments>
<pubDate>Thu, 24 Sep 2015 01:54:35 GMT</pubDate>
<description><description/>
<item-1>headline</item-1>
<item-2>government</item-2>
<item-3>privacy</item-3>
<item-4>usa</item-4>
</item>
<item>
<title>Title2</title>
<guid isPermaLink="true">http</guid>
<comments>comment</comments>
<pubDate>Sat, 26 Sep 2015 03:14:57 GMT</pubDate>
<description>Description</description>
<category><category/>
</item>
這是你的輸入錯誤:
是的,絕對是一個錯字,感謝您指出,這是我第一次在這裏發佈,並耗盡嘗試解決我的XML問題後耗盡。 – z0mb1e