我一直在研究XSLT 1.0中的Muenchian分组示例,特别是此示例here。但是我无法让它在更复杂的XML结构上工作。
我的XML目前看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ContestResults>
<Contests>
<Contest sportId="35">
<Sport>Beach Volleyball</Sport>
<Event>Men's</Event>
<Ranks>
<Rank position="1" eventId="1">
<Athlete>Athlete 1a / Athlete 2a [GER]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="1">
<Athlete>Athlete 1b / Athlete 2b [NED]</Athlete>
<Result>0</Result>
</Rank>
</Ranks>
</Contest>
<Contest sportId="32">
<Sport>Tennis</Sport>
<Event>Women's Singles</Event>
<Ranks>
<Rank position="1" eventId="2">
<Athlete>Tennis Athlete 1</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="2">
<Athlete>Tennis Athlete 2</Athlete>
<Result>1</Result>
</Rank>
</Ranks>
</Contest>
<Contest sportId="35">
<Sport>Beach Volleyball</Sport>
<Event>Men's</Event>
<Ranks>
<Rank position="1" eventId="3">
<Athlete>Athlete 3a / Athlete 4a [AUT]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="3">
<Athlete>Athlete 3b / Athlete 4b [SUI]</Athlete>
<Result>0</Result>
</Rank>
</Ranks>
</Contest>
</Contests>
</ContestResults>
但是,我想在相同的Ranks父级下将Rank节点分组,因为它们具有相同的Sport和Event。所以我希望结果看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ContestResults>
<Contests>
<Contest sportId="35">
<Sport>Beach Volleyball</Sport>
<Event>Men's</Event>
<Ranks>
<Rank position="1" eventId="1">
<Athlete>Athlete 1a / Athlete 2a [GER]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="1">
<Athlete>Athlete 1b / Athlete 2b [NED]</Athlete>
<Result>0</Result>
</Rank>
<Rank position="1" eventId="3">
<Athlete>Athlete 3a / Athlete 4a [AUT]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="3">
<Athlete>Athlete 3b / Athlete 4b [SUI]</Athlete>
<Result>0</Result>
</Rank>
</Ranks>
</Contest>
<Contest sportId="32">
<Sport>Tennis</Sport>
<Event>Women's Singles</Event>
<Ranks>
<Rank position="1" eventId="2">
<Athlete>Tennis Athlete 1</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="2">
<Athlete>Tennis Athlete 2</Athlete>
<Result>1</Result>
</Rank>
</Ranks>
</Contest>
</Contests>
</ContestResults>
我只是有点迷失如何做到这一点,因为唯一的其他例子正在处理一个更简单的结构,我不确定它是否可能或我的密钥和模板需要如何构建来执行此操作。任何人都可以提供一些如何实现这一目标的例子吗?
任何建议都将受到赞赏。
答案 0 :(得分:0)
此转化:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kContestById" match="Contest" use="@sportId"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Contests">
<Contests>
<xsl:apply-templates/>
</Contests>
</xsl:template>
<xsl:template match=
"Contest
[not(generate-id()
=
generate-id(key('kContestById', @sportId)[1]))
]"/>
<xsl:template match="Ranks">
<Ranks>
<xsl:apply-templates select="key('kContestById', ../@sportId)/Ranks/Rank"/>
</Ranks>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时:
<ContestResults>
<Contests>
<Contest sportId="35">
<Sport>Beach Volleyball</Sport>
<Event>Men's</Event>
<Ranks>
<Rank position="1" eventId="1">
<Athlete>Athlete 1a / Athlete 2a [GER]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="1">
<Athlete>Athlete 1b / Athlete 2b [NED]</Athlete>
<Result>0</Result>
</Rank>
</Ranks>
</Contest>
<Contest sportId="32">
<Sport>Tennis</Sport>
<Event>Women's Singles</Event>
<Ranks>
<Rank position="1" eventId="2">
<Athlete>Tennis Athlete 1</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="2">
<Athlete>Tennis Athlete 2</Athlete>
<Result>1</Result>
</Rank>
</Ranks>
</Contest>
<Contest sportId="35">
<Sport>Beach Volleyball</Sport>
<Event>Men's</Event>
<Ranks>
<Rank position="1" eventId="3">
<Athlete>Athlete 3a / Athlete 4a [AUT]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="3">
<Athlete>Athlete 3b / Athlete 4b [SUI]</Athlete>
<Result>0</Result>
</Rank>
</Ranks>
</Contest>
</Contests>
</ContestResults>
会产生想要的正确结果:
<ContestResults>
<Contests>
<Contest sportId="35">
<Sport>Beach Volleyball</Sport>
<Event>Men's</Event>
<Ranks>
<Rank position="1" eventId="1">
<Athlete>Athlete 1a / Athlete 2a [GER]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="1">
<Athlete>Athlete 1b / Athlete 2b [NED]</Athlete>
<Result>0</Result>
</Rank>
<Rank position="1" eventId="3">
<Athlete>Athlete 3a / Athlete 4a [AUT]</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="3">
<Athlete>Athlete 3b / Athlete 4b [SUI]</Athlete>
<Result>0</Result>
</Rank>
</Ranks>
</Contest>
<Contest sportId="32">
<Sport>Tennis</Sport>
<Event>Women's Singles</Event>
<Ranks>
<Rank position="1" eventId="2">
<Athlete>Tennis Athlete 1</Athlete>
<Result>2</Result>
</Rank>
<Rank position="2" eventId="2">
<Athlete>Tennis Athlete 2</Athlete>
<Result>1</Result>
</Rank>
</Ranks>
</Contest>
</Contests>
</ContestResults>
<强>解释强>:
正确使用 Muenchian grouping method 并覆盖 identity rule 。