我使用以下XSLT文件转换了XML文件:
[['0'] ['0'] ['0'] ['0,0,0,0,0,0,0,0,0,0,0']
['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0']
['0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0']]
结果如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<catalog><xsl:for-each select="catalog/cd">
<cd>
<title><xsl:value-of select="title"/></title>
<artist><xsl:value-of select="artist"/></artist>
<country><xsl:value-of select="country"/></country>
<company><xsl:value-of select="company"/></company>
<price><xsl:value-of select="price"/></price>
<years>
<year><xsl:value-of select="year-1"/></year>
<year><xsl:value-of select="year-2"/></year>
<year><xsl:value-of select="year-3"/></year>
</years>
</cd>
</xsl:for-each></catalog>
</xsl:template>
</xsl:stylesheet>
我不想输出TAG
<catalog><cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<years>
<year>1985</year>
<year>1986</year>
<year>1987</year>
</years>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<years>
<year>1988</year>
<year>1989</year>
<year></year>
</years>
</cd>
如果单个字段year-1,year-2或year-3的字段为空
我尝试了一些在其他帖子中找到的代码例程,但是它们 似乎所有地方都存在问题。
答案 0 :(得分:0)
您需要做的就是在创建节点并选择值之前,通过xsl:if
检查该字段是否为空。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<catalog>
<xsl:for-each select="catalog/cd">
<cd>
<title>
<xsl:value-of select="title"/>
</title>
<artist>
<xsl:value-of select="artist"/>
</artist>
<country>
<xsl:value-of select="country"/>
</country>
<company>
<xsl:value-of select="company"/>
</company>
<price>
<xsl:value-of select="price"/>
</price>
<years>
<xsl:if test="year-1!=''">
<year>
<xsl:value-of select="year-1"/>
</year>
</xsl:if>
<xsl:if test="year-2!=''">
<year>
<xsl:value-of select="year-2"/>
</year>
</xsl:if>
<xsl:if test="year-3!=''">
<year>
<xsl:value-of select="year-3"/>
</year>
</xsl:if>
</years>
</cd>
</xsl:for-each>
</catalog>
</xsl:template>
</xsl:stylesheet>
输入
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year-1>1985</year-1>
<year-2>1986</year-2>
<year-3>1987</year-3>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year-1>1988</year-1>
<year-2>1989</year-2>
<year-3></year-3>
</cd>
</catalog>
产生输出
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<years>
<year>1985</year>
<year>1986</year>
<year>1987</year>
</years>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<years>
<year>1988</year>
<year>1989</year>
</years>
</cd>
</catalog>
答案 1 :(得分:0)
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(string(.))]" />
您也可以使用它。