如何为输出提供正确的格式? XSLT中缺少什么? 看看下面的例子。提前感谢您的帮助。
XML:
<?xml version="1.0" encoding="UTF-8"?>
<FED xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<POI>
<Id>56241</Id>
<POI_Contact>
<CategoryID>museum</CategoryID>
<Name>Centre Pompidou</Name>
</POI_Contact>
</POI>
<POI>
<Id>141670</Id>
<POI_Contact>
<CategoryID>museum</CategoryID>
<Name>Espace Culturel Saint-Exupéry</Name>
</POI_Contact>
</POI>
<POI>
<Id>56242</Id>
<POI_Contact>
<CategoryID>museum</CategoryID>
<Name>Musée du Louvre</Name>
</POI_Contact>
</POI>
</FED>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
ID;Category;Name
<xsl:for-each select="FED/POI">
<xsl:value-of select="Id"/>;
<xsl:value-of select="POI_Contact/CategoryID"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出:
ID;Category;Name
56241;
museum
141670;
museum
56242;
museum
输出应如下所示:
ID;分类;名称
56241;博物馆
141670;博物馆
56242;博物馆
答案 0 :(得分:0)
指定文字文本的问题在于,整个文本节点(包括周围的空格)都会传递给输出。
尝试改为:
<xsl:template match="/">
<xsl:text>ID;Category;Name </xsl:text>
<xsl:for-each select="FED/POI">
<xsl:value-of select="Id"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="POI_Contact/CategoryID"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
P.S。您的标题显示ID;Category;Name
,但您没有输出Name
。