<BookList>
<Book>
<History>
<Type>history</Type>
<Prize>123</Prize>
<Publication>``
<Name>YEAP1</Name>
</Publication>
<RNumber Type="VolumeNumber">11111</RNumber>
<RNumber Type="SupplementNumber">123456</RNumber>
</History>
<chemistry>
<Type>chemistry</Type>
<Prize>333</Prize>
<Publication>
<Name>YEAP</Name>
</Publication>
<RNumber Type="VolumeNumber">11111</RNumber>
<RNumber Type="SupplementNumber">45454</RNumber>
</chemistry>
......
</Book>
</BookList>
有重复的VolumnNumber 11111.如何使用xslt检查BoolList xml中的重复VolumnNumber。请帮忙解决这个问题
答案 0 :(得分:2)
<强>予。这可以使用单个XPath表达式:
找到 false()
or
/*/*/*/RNumber
[@Type='VolumeNumber'
and
. = ../preceding-sibling::*
/RNumber[@Type='VolumeNumber']
]
这是一个完整的XSLT转换,用于评估此XPath表达式并输出此评估的结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
Duplicate volume numbers exist: <xsl:text/>
<xsl:value-of select=
"false()
or
/*/*/*/RNumber
[@Type='VolumeNumber'
and
. = ../preceding-sibling::*
/RNumber[@Type='VolumeNumber']
]
"/>
</xsl:template>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<BookList>
<Book>
<History>
<Type>history</Type>
<Prize>123</Prize>
<Publication>``
<Name>YEAP1</Name>
</Publication>
<RNumber Type="VolumeNumber">11111</RNumber>
<RNumber Type="SupplementNumber">123456</RNumber>
</History>
<chemistry>
<Type>chemistry</Type>
<Prize>333</Prize>
<Publication>
<Name>YEAP</Name>
</Publication>
<RNumber Type="VolumeNumber">11111</RNumber>
<RNumber Type="SupplementNumber">45454</RNumber>
</chemistry>
......
</Book>
</BookList>
产生了想要的正确结果:
Duplicate volume numbers exist: true
<强> II。使用密钥的解决方案(通常更有效):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kVolNum" match="RNumber[@Type='VolumeNumber']"
use="."/>
<xsl:template match="/">
Duplicate volume numbers exist: <xsl:text/>
<xsl:value-of select=
"false()
or
/*/*/*/RNumber
[@Type='VolumeNumber'
and
key('kVolNum',.)[2]
]"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于同一XML文档(上图)时,会产生相同的正确结果:
Duplicate volume numbers exist: true