【发布时间】:2019-11-04 07:21:02
【问题描述】:
我有以下 XML 文档,喜欢按组标签对书籍进行分组,并使用 Java 和 XSLT 1 将组的所有 ID(标题)组合到 csv。
此外,我还希望有一个摘要元素,其中包含丛书 (SeriesInfo) 的所有共享信息以及每组中的两个元素;一个(例如 Titles)包含该组的所有标题(ID),以逗号分隔(csv),一个(例如 AnyTitle)包含任何标题(哪个无关紧要,第一个或最后一个都可以)。
我已经设法通过 Muenchian Grouping 进行分组,但不知道如何获取 csv 和 any 元素。我对此进行了一些研究,但我发现的解决方案要么非常具体,要么使用 XSLT 2 或更高版本。
源 XML
<?xml version="1.0" encoding="UTF-8"?>
<Books>
<Book>
<Title>Harry Potter and the philosopher's stone</Title>
<Group>Harry Potter</Group>
<Author>J.K.R.</Author>
<Pages>650</Pages>
</Book>
<Book>
<Title>Harry Potter and the chamber of secrets</Title>
<Group>Harry Potter</Group>
<Author>J.K.R.</Author>
<Pages>700</Pages>
</Book>
<Book>
<Title>Lord of the Rings complete edition</Title>
<Group>Lord of the Rings</Group>
<Author>J.R.R. Tolkien</Author>
<Pages>2500</Pages>
</Book>
</Books>
目标 XML
<?xml version="1.0" encoding="UTF-8"?>
<Serieses>
<Series>
<Group>Harry Potter</Group>
<Titles>Harry Potter and the philosopher's stone,Harry Potter and the chamber of secrets</Titles>
<AnyTitle>Harry Potter and the chamber of secrets</AnyTitle>
<Books>
<Book>
<Title>Harry Potter and the philosopher's stone</Title>
<Group>Harry Potter</Group>
<Pages>650</Pages>
</Book>
<Book>
<Title>Harry Potter and the chamber of secrets</Title>
<Group>Harry Potter</Group>
<Pages>700</Pages>
</Book>
</Books>
<SeriesInfo>
<Author>J.K.R.</Author>
<Group>Harry Potter</Group>
</SeriesInfo>
</Series>
<Series>
<Group>Lord of the Rings</Group>
<Titles>Lord of the Rings complete edition</Titles>
<AnyTitle>Lord of the Rings complete edition</AnyTitle>
<Books>
<Book>
<Title>Lord of the Rings complete edition</Title>
<Group>Lord of the Rings</Group>
<Pages>2500</Pages>
</Book>
</Books>
<SeriesInfo>
<Author>J.R.R. Tolkien</Author>
<Group>Lord of the Rings</Group>
</SeriesInfo>
</Series>
</Serieses>
使用以下 XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="book-by-name" match="Book" use="Group" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Books">
<Serieses>
<xsl:apply-templates
select="Book[generate-id() = generate-id(key('book-by-name', Group)[1])]"
mode="group" />
</Serieses>
</xsl:template>
<xsl:template match="Book" mode="group">
<Series>
<xsl:copy-of select="Group" />
<Books>
<xsl:apply-templates
select="key('book-by-name', Group)" />
</Books>
<SeriesInfo>
<xsl:copy-of select="Author" />
<xsl:copy-of select="Group" />
</SeriesInfo>
</Series>
</xsl:template>
<xsl:template match="Book">
<Book>
<xsl:apply-templates
select="node()[self::Title|self::Group|self::Pages]" />
</Book>
</xsl:template>
</xsl:stylesheet>
我能够得到以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<Serieses>
<Series>
<Group>Harry Potter</Group>
<Books>
<Book>
<Title>Harry Potter and the philosopher's stone</Title>
<Group>Harry Potter</Group>
<Pages>650</Pages>
</Book>
<Book>
<Title>Harry Potter and the chamber of secrets</Title>
<Group>Harry Potter</Group>
<Pages>700</Pages>
</Book>
</Books>
<SeriesInfo>
<Author>J.K.R.</Author>
<Group>Harry Potter</Group>
</SeriesInfo>
</Series>
<Series>
<Group>Lord of the Rings</Group>
<Books>
<Book>
<Title>Lord of the Rings complete edition</Title>
<Group>Lord of the Rings</Group>
<Pages>2500</Pages>
</Book>
</Books>
<SeriesInfo>
<Author>J.R.R. Tolkien</Author>
<Group>Lord of the Rings</Group>
</SeriesInfo>
</Series>
</Serieses>
使用任何更新版本的 XSLT 对我来说并没有真正的帮助,因为我需要依赖标准库。
编辑: 澄清我的任何标题的意思:没关系,第一个或最后一个都可以。
【问题讨论】:
-
“任何标题”究竟是什么意思?如果该组包含许多书籍,您如何决定选择哪本书的标题?
-
任何标题都意味着可以任意选择的任何人。对我来说,它是哪个标题并不重要(第一个,最后一个,随机选择);我只需要任何人。 First/Last 对我来说似乎最有意义,但不是必需的。
标签: java xml xslt xslt-1.0 xslt-grouping