使用XSLT从XML文件中删除重复值(Deleting duplicate values from XML file using XSLT)

编程入门 行业动态 更新时间:2024-10-27 06:26:12
使用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

本文发布于:2023-08-01 11:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357765.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   XML   XSLT   Deleting   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!