使用XSL转换合并两个XML文件



我一直在尝试将两个xml文件合并到另一个xml中,并使用XSL将两者的输出组合在一起,但一直未能成功。xml相当大,所以我只包含一小部分。我真的没法完成这项任务。我可以转换一个XML文件,但合并两个是我从未做过的事情,也找不到与我的具体案例相关的太多信息。

XML1:

<Games>
<Game>
<Date>01/05/2019</Date>
<PlayerID>454asdsad</PlayerID>
<Place>1</Place>
<GameID>CpsQf125AFy</GameID>
<Payment currency="gbp">50</Payment>
</Game>
.....repeats the above many times with different values.
</Games>

XML2:

<Players>
<Player>
<Title>Mr</Title>
<Lastname>Doe</Lastname>
<Firstname>John</Firstname>
<IDnumber>454asdsad</IDnumber>
<Address>Streetname</Address>
</Player>
.....repeats the above many times with different values.
</Players>

预期结果:

<Games>
<Place>
<Date>
<Game>
<Title>Mr</prefix>
<Lastname>Doe</Lastname>
<Firstname>John</Firstname>
<IDnumber>454asdsad</IDnumber>
<Address>Streetname</Address>
<Date>01/05/2019</Date>
<PlayerID>454asdsad</Player>
<Place>1</Place>
<GameID>CpsQf125AFy</GameID>
<Payment currency="gbp">50</Payment>
</Game>
</Date>
<Date> ...if there are more dates is the same place as above.
<Game>
....information
</Game>
</Date>
</Place>
<Place> ...another place
<Date>
<Game>
...all the information like above, with the appropriate next values from both XML's.
</Game>
</Date>
<Date> ...if there are more dates is the same place as above.
<Game>
....information
</Game>
<Date>
</Place>
...repeats same thing and format until the end.
</Games>

这有两个部分:

  • 分组,先在现场,然后在日期

  • 加入、从由personID选择的第二文件获取数据。

分组基本上是

<xsl:for-each-group select="Game" group-by="Place">
<Place>
<xsl:for-each-group select="current-group()" group-by="Date">
<Date>
<xsl:for-each select="current-group()">
<Game>
XXXXX
<xsl:copy-of select="*"/>

在XXXXX,你需要进行连接,这基本上是

<xsl:copy-of select="key('player-id', PlayerID, $players-xml')/*"/>

其中$players.xml是players.xml文档,密钥定义为

<xsl:key name="player-id" match="Player" use="IDNumber"/>

如果您了解for-each-group,那么从两个文档中提取元素应该没有问题,例如<xsl:for-each-group select="/Games/Game, doc('file2.xml')/Players/Player" group-by="PlayerID, IDnumber">,分组关键字将按该元素存在的PlayerID和该元素存在时的IDnumber,只要没有同时具有两个子元素的元素,则该方法应该是安全的,或者可以更正为group-by="(PlayerID, IDnumber)[1]"以选择一个关键字(如果不存在(。

如果还需要按Place分组,则可以嵌套另一个for-each-group select="current-group()" group-by="Place"或使用复合密钥。

最新更新