【问题标题】:XSLT 1 Grouping and combining IDs to csvXSLT 1 将 ID 分组和组合到 csv
【发布时间】: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


【解决方案1】:

这是您可以查看的一种方式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="book-by-group" match="Book" use="Group" />

<!-- identity transform -->
<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-group', Group)[1])]" mode="group" />
    </Serieses>
</xsl:template>

<xsl:template match="Book" mode="group">
    <xsl:variable name="current-group" select="key('book-by-group', Group)" />
    <Series>
        <xsl:apply-templates select="Group" />
        <Titles>
            <xsl:apply-templates select="$current-group" mode="Title"/>
        </Titles>
        <AnyTitle>
            <xsl:value-of select="$current-group[1]/Title"/>
        </AnyTitle>
        <Books>
            <xsl:apply-templates select="$current-group" />
        </Books>
        <SeriesInfo>
            <xsl:apply-templates select="Author" />
            <xsl:apply-templates select="Group" />
        </SeriesInfo>
    </Series>
</xsl:template>

<xsl:template match="Book">
    <Book>
        <xsl:apply-templates select="Title | Group| Pages" />
    </Book>
</xsl:template>

<xsl:template match="Book" mode="Title">
    <xsl:value-of select="Title"/>
    <xsl:if test="position() != last()">,</xsl:if>
</xsl:template>

</xsl:stylesheet>

这将使用组标题的逗号分隔列表填充Titles 元素。对于AnyTitle 元素,我选择了该组中第一本书的标题。


就个人而言,我更愿意将整个内容缩短为:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="book-by-group" match="Book" use="Group" />

<xsl:template match="/Books">
    <Serieses>
        <xsl:for-each select="Book[generate-id() = generate-id(key('book-by-group', Group)[1])]">
            <xsl:variable name="current-group" select="key('book-by-group', Group)" />
            <Series>
                <xsl:copy-of select="Group" />
                <Titles>
                    <xsl:for-each select="$current-group">
                        <xsl:value-of select="Title"/>
                        <xsl:if test="position() != last()">,</xsl:if>
                    </xsl:for-each>
                </Titles>
                <AnyTitle>
                    <xsl:value-of select="$current-group[1]/Title"/>
                </AnyTitle>
                <Books>
                    <xsl:for-each select="$current-group">
                        <xsl:copy>
                            <xsl:copy-of select="Title | Group| Pages" />
                        </xsl:copy>
                    </xsl:for-each>
                </Books>
                <SeriesInfo>
                    <xsl:copy-of select="Author" />
                    <xsl:copy-of select="Group" />
                </SeriesInfo>
            </Series>
        </xsl:for-each>
    </Serieses>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 不幸的是,您的解决方案省略了最后一个标题,因此它们生成 &lt;Titles&gt;Harry Potter and the philosopher's stone,&lt;/Titles&gt; 而不是 &lt;Titles&gt;Harry Potter and the philosopher's stone,Harry Potter and the chamber of secrets&lt;/Titles&gt;。但是,通过将test="position() != last()" 测试条件设置为始终为真,例如2 != 1,我得到&lt;Titles&gt;Harry Potter and the philosopher's stone,Harry Potter and the chamber of secrets,&lt;/Titles&gt;,这完全没问题,因为我可以截断最后一个逗号。
  • 我使用Java的默认实现(javax.xml.transform.Transformer)。也许它与其他实现有点不同。但是,您的解决方案显然似乎是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多