使用XSLT在XML文件中显示重复值?

时间:2014-08-13 02:57:44

标签: xml xslt

我是XSLT的新手。输入文件下方的标记中包含重复值,我的要求是使用XSLT显示<Rip><PGS>的所有重复值。

<Rip>是随机值,它将在另一个标记中重复。我们需要确定<RIP>标记值与重复项匹配的位置。

如何使用XSLT仅显示重复的标记<PGS>

输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<Rows>
    <PGS>
    <Rip>93S981371 </Rip>
    <Settled>NO</Settled>
    <MaturityDate>1/1/2025</MaturityDate>
    <RemainingFace>105000000</RemainingFace>
    <CurrentFace>0</CurrentFace>
    </PGS>
    <PGS>
    <Rip>02R030483 </Rip>
    <Settled>YES</Settled>
    <MaturityDate>9/8/2025</MaturityDate>
    <Remaining-Face>285000000</RemainingFace>
    <CurrentFace>0</CurrentFace>
    </PGS>
    <PGS>
    <Rip>77344444 </Rip>
    <Settled>YES</Settled>
    <MaturityDate>9/8/2025</MaturityDate>
    <Remaining-Face>285000000</RemainingFace>
    <CurrentFace>0</CurrentFace>
    </PGS>
    <PGS>
    <Rip>02R030483 </Rip>
    <Settled>NO</Settled>
    <MaturityDate>9/8/2025</MaturityDate>
    <Remaining-Face>285000000</RemainingFace>
    <CurrentFace>0</CurrentFace>
    </PGS>
    <PGS>
    <Rip>93S981371 </Rip>
    <Settled>YES</Settled>
    <MaturityDate>1/1/2025</MaturityDate>
    <Remaining-Face>405000000</RemainingFace>
    <CurrentFace>0</CurrentFace>
    </PGS>
</Rows>

预期的输出文件:

<?xml version="1.0"?>
<Rows>
<PGS>
<Rip>93S981371 </Rip>
<Settled>NO</Settled>
<MaturityDate>1/1/2025</MaturityDate>
<RemainingFace>105000000</RemainingFace>
<CurrentFace>0</CurrentFace>
</PGS>
 <PGS>
<Rip>93S981371 </Rip>
<Settled>YES</Settled>
<MaturityDate>1/1/2025</MaturityDate>
<Remaining-Face>405000000</RemainingFace>
<CurrentFace>0</CurrentFace>
</PGS>
<PGS>
<Rip>02R030483 </Rip>
<Settled>YES</Settled>
<MaturityDate>9/8/2025</MaturityDate>
<Remaining-Face>285000000</RemainingFace>
<CurrentFace>0</CurrentFace>
</PGS>
</PGS>
<Rip>02R030483 </Rip>
<Settled>NO</Settled>
<MaturityDate>9/8/2025</MaturityDate>
<Remaining-Face>285000000</RemainingFace>
<CurrentFace>0</CurrentFace>
</PGS>

1 个答案:

答案 0 :(得分:0)

你的问题的答案是:

首先定义一个密钥,用PGS标识Rip,例如

<xsl:key name="pgs" match="PGS" use="Rip" />

然后匹配(或复制)具有多个“相关”值的PGS元素:

PGS[count(key('pgs', Rip)) > 1]

即。它们与另一个PGS元素相匹配。