我输入了xml
<?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>
<year>1981</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1985</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
结果XML
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>MyCountry</country>
<company>MyCompany</company>
<price>10.90</price>
<year>1981</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1985</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>MyCountry</country>
<company>MyCompany</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
答案 0 :(得分:0)
有多种选择可以满足您的需求。您需要从identity template
开始,它将复制输入XML原样输出。
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
接下来,您可以声明单独的模板,用于在country
处修改company
和year != 1985
。
<xsl:template match="cd[year != '1985']/country">
<country>MyCountry</country>
</xsl:template>
<xsl:template match="cd[year != '1985']/company">
<company>MyCompany</company>
</xsl:template>
另一种选择是在其子元素cd
的单个模板year != 1985
中更改两个元素的值。
<xsl:template match="cd[year != '1985']">
<xsl:copy>
<xsl:apply-templates select="title | artist" />
<country>MyCountry</country>
<company>MyCompany</company>
<xsl:apply-templates select="price | year" />
</xsl:copy>
</xsl:template>
希望这会有所帮助。但是,您需要在线上和做一些有关XSLT的教程。
答案 1 :(得分:0)
<xsl:template match="catalog">
<catalog>
<xsl:for-each select="cd">
<cd>
<title>
<xsl:value-of select="title"/>
</title>
<artist>
<xsl:value-of select="artist"/>
</artist>
<xsl:choose>
<xsl:when test="not(year = 1985)">
<country>MY Country</country>
<company>My Company</company>
</xsl:when>
<xsl:otherwise>
<country><xsl:value-of select="country"/></country>
<company><xsl:value-of select="company"/></company>
</xsl:otherwise>
</xsl:choose>
<price><xsl:value-of select="price"/></price>
<year><xsl:value-of select="year"/></year>
</cd>
</xsl:for-each>
</catalog>
</xsl:template>
Check it, I may be helful for you