我將相同的多標記元素轉換爲不同的記錄。 在這我使用XSLT。 我需要XSLT幫助。 我得到第一個標籤結果是正確的,螺母idont知道爲什麼第二個標籤沒有得到。使用XSLT將多級XML轉換爲平面XML
說明:: * 在第一張CD:單個atrist到許多標題。我想根據標題將其分成不同的標籤。 第二張CD:第一個藝術家的第一個標題和第二個藝術家的第二個標題。我也需要根據標題和藝術家的不同(兩個)CD標籤來製作[第一個標題 - 第一個藝術家和第二個標題 - 第二個aritst]。 *
我的源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>
任何人都可以幫助我嗎?
一旦請參閱標籤。它來了兩次。隱藏你的 邦妮 泰勒 英國 CBS唱片 9.90 1988年 心臟 邦妮 泰勒 英國 CBS唱片 9.90 1988年 –
2012-03-20 13:12:59
@ ram.bi:我明白了,所以你想要的比簡單的粉碎更加複雜,不同的藝術家需要結合不同的標題。但是,你必須編輯這個問題,並且展開哪個藝術家應該與哪個標題匹配。兩位藝術家可能會唱同一首歌曲(我認爲),或者兩位藝術家都唱這兩首歌曲,或者第一位歌手唱第二首歌曲(想象藝術家和歌曲是按字母順序排列的,請編輯問題並指定確切的規則 – 2012-03-20 13:37:18
嗨Dimitre,謝謝你的迴應,我添加了解釋,可以請把我放在正確的方向。 – 2012-03-21 09:46:47