我有一个带有以下示例的xml
gte_score_001
gte_score_01
gte_score_1
gte_score_10
我想使用xslt使用以下内容创建xml:
<Message>
<Person>
<Key>111</Key>
<Name>John</Name>
<Age>20</Age>
</Person>
<Person>
<Key>112</Key>
<Name>Alex</Name>
<Age>20</Age>
</Person>
<Person>
<Key>113</Key>
<Name>Sean</Name>
<Age>20</Age>
</Person>
<Car>
<Key>111</Key>
<Make>Toyota</Make>
<Model>Camry</Model>
</Car>
<Car>
<Key>111</Key>
<Make>Toyota</Make>
<Model>Corolla</Model>
</Car>
<Car>
<Key>112</Key>
<Make>Honda</Make>
<Model>Civic</Model>
</Car>
<Car>
<Key>113</Key>
<Make>Lexus</Make>
<Model>G300</Model>
</Car>
</Message>
我想为每个键创建一个消息段,并在其中为每个键分别设置<Message>
<Person>
<Key>111</Key>
<Name>John</Name>
<Age>20</Age>
</Person>
<Car>
<Key>111</Key>
<Make>Toyota</Make>
<Model>Camry</Model>
</Car>
<Car>
<Key>111</Key>
<Make>Toyota</Make>
<Model>Corolla</Model>
</Car>
</Message>
和<person>
段。如何使用xslt 2.0做到这一点?
答案 0 :(得分:0)
<xsl:template match="Message">
<xsl:for-each select="Person[Key = following-sibling::Car/Key]">
<Message>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::Car[Key = current()/Key]"/>
</Message>
</xsl:for-each>
</xsl:template>
Check it
答案 1 :(得分:0)
要按照评论中的建议使用for-each-group
,请使用
<xsl:template match="Message">
<xsl:for-each-group select="*" group-by="Key">
<Message>
<xsl:copy-of select="current-group()"/>
</Message>
</xsl:for-each-group>
</xsl:template>
完整的样式表是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:output indent="yes"/>
<xsl:template match="Message">
<xsl:for-each-group select="*" group-by="Key">
<Message>
<xsl:copy-of select="current-group()"/>
</Message>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>