【发布时间】:2012-04-04 21:48:34
【问题描述】:
我正在将相同的多标签元素转换为不同的记录。 在这我使用 XSLT。 我需要 XSLT 方面的帮助。 我得到的第一个标签结果是正确的,坚果不知道为什么第二个标签没有得到。
解释:: *在第一张 CD :: 许多标题的单曲作者。我想根据标题将其拆分为不同的标签。 在第二张 CD 中::第一位艺术家的第一个标题和第二位艺术家的第二个标题。我还需要基于标题和艺术家的不同(两个)CD标签。[第一个标题-第一个艺术家和第二个标题-第二个艺术家]。*
我的源 XML 如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
<title>Burlesque</title>
<title>Empire</title>
<title>Emp</title>
<title>Em</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your</title>
<title>heart</title>
<artist>Bonnie</artist>
<artist> Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title/>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
XSLT ::
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:key name="k" match="title" use="text()"/>
<xsl:key name="l" match="artist" use="text()"/>
<xsl:template match="/">
<catalog>
<xsl:apply-templates select="//cd/title | artist[not(node() = preceding-sibling::node())]"/>
</catalog>
</xsl:template>
<xsl:template match="//cd">
<xsl:param name="title" select="title"/>
<xsl:param name="artist" select="artist"/>
<cd>
<xsl:copy-of select="key('k', $title)[not(node() = preceding-sibling::node())]"/>
<xsl:copy-of select="key('l', $artist)[not(node() = preceding-sibling::node())]"/>
<xsl:copy-of select="./*[name() != 'title' and 'artist']"/>
</cd>
</xsl:template>
<xsl:template match="title">
<xsl:apply-templates select="..">
<xsl:with-param name="title" select="."/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
我得到了什么::
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Burlesque</title>
<artist>Bob Dylan</artist>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Empire</title>
<artist>Bob Dylan</artist>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Emp</title>
<artist>Bob Dylan</artist>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Em</title>
<artist>Bob Dylan</artist>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your</title>
<artist>Bonnie</artist>
<artist> Tyler</artist>
<artist>Bonnie</artist>
<artist> Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>heart</title>
<artist>Bonnie</artist>
<artist> Tyler</artist>
<artist>Bonnie</artist>
<artist> Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<artist>Dolly Parton</artist>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
我需要什么::
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Empire</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Emp</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Em</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your</title>
<artist>Bonnie</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>heart</title>
<artist> Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<artist>Dolly Parton</artist>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
谁能帮帮我???
【问题讨论】: