使用 XSL 转换合并两个 XML 文件
Merging two XML files using XSL transformation
我一直在尝试将两个 xml 文件合并到另一个 XML 中,并使用 XSL 合并两个文件的输出,但未能成功。 xml 很大,所以我只包含一小部分。
我真的迷失了这项任务。我可以转换单个 XML 文件,但合并两个是我从未做过的事情,并且找不到与我的具体案例相关的太多信息。
XML 1:
<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>
XML 2:
<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>
如果您知道 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]"
到 select 一键。
如果还需要按 Place
分组,您可以嵌套另一个 for-each-group select="current-group()" group-by="Place"
或使用复合键。
这有两部分:
分组,先到位再到日期
加入,从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"/>
我一直在尝试将两个 xml 文件合并到另一个 XML 中,并使用 XSL 合并两个文件的输出,但未能成功。 xml 很大,所以我只包含一小部分。 我真的迷失了这项任务。我可以转换单个 XML 文件,但合并两个是我从未做过的事情,并且找不到与我的具体案例相关的太多信息。
XML 1:
<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>
XML 2:
<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>
如果您知道 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]"
到 select 一键。
如果还需要按 Place
分组,您可以嵌套另一个 for-each-group select="current-group()" group-by="Place"
或使用复合键。
这有两部分:
分组,先到位再到日期
加入,从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"/>