使用XSLT从XML文件中删除重复值(Deleting duplicate values from XML file using XSLT)
我试图使用XSLT从XML文件中删除重复项。 输入是这样的:
<catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd>所需的输出是:
<catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd>基本上我想删除重复的记录。 我怎么做到这一点?
I am trying to remove duplicates from a XML file using XSLT. Input is something like this :
<catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd>The required output is :
<catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd>Basically i am trying to remove duplicate records. How do i get this done ?
最满意答案
假设所有cd元素具有相同顺序的子元素和条形字符| 不是您可以使用的任何价值的一部分
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <xsl:output indent="yes"/> <xsl:template match="/*"> <xsl:copy> <xsl:for-each-group select="cd" group-by="string-join(*, '|')"> <xsl:copy-of select="."/> </xsl:for-each-group> </xsl:copy> </xsl:template> </xsl:stylesheet>显然,如果该条形字符可以在任何值内,您可以使用不同的字符来分隔值。
Assuming all cd elements have the same child elements in the same order and the bar character | is not part of any value you can use
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <xsl:output indent="yes"/> <xsl:template match="/*"> <xsl:copy> <xsl:for-each-group select="cd" group-by="string-join(*, '|')"> <xsl:copy-of select="."/> </xsl:for-each-group> </xsl:copy> </xsl:template> </xsl:stylesheet>Obviously if that bar character can be inside any value you could use a different character to separate the values.
更多推荐
<cd>,cd>,<catalog>,trying,电脑培训,计算机培训,IT培训"/> <meta
发布评论