使用XSL或XPATH获取已排序的不同元素

时间:2012-10-10 07:19:19

标签: xml xslt xpath distinct

鉴于此源XML:

<Objects>
    <Object>
        <Relations>
            <Relation>
                <Placements>
                    <Placement>
                        <Page>6</Page>
                        <Element>body</Element>
                        <FrameOrder>3</FrameOrder>
                        <PageSequence>2</PageSequence>
                        <PageNumber>6</PageNumber>
                    </Placement>
                    <Placement>
                        <Page>1</Page>
                        <Element>body</Element>
                        <FrameOrder>0</FrameOrder>
                        <PageSequence>1</PageSequence>
                        <PageNumber>1</PageNumber>
                    </Placement>
                    <Placement>
                        <Page>6</Page>
                        <Element>body</Element>
                        <FrameOrder>4</FrameOrder>
                        <PageSequence>2</PageSequence>
                        <PageNumber>6</PageNumber>
                    </Placement>
                    <Placement>
                        <Page>1</Page>
                        <Element>head</Element>
                        <FrameOrder>0</FrameOrder>
                        <PageSequence>1</PageSequence>
                        <PageNumber>1</PageNumber>
                    </Placement>
                    <Placement>
                        <Page>1</Page>
                        <Element>body</Element>
                        <FrameOrder>2</FrameOrder>
                        <PageSequence>1</PageSequence>
                        <PageNumber>1</PageNumber>
                    </Placement>
                    <Placement>
                        <Page>1</Page>
                        <Element>body</Element>
                        <FrameOrder>1</FrameOrder>
                        <PageSequence>1</PageSequence>
                        <PageNumber>1</PageNumber>
                    </Placement>
                </Placements>
            </Relation>
        </Relations>
    </Object>
</Objects>

我正在尝试按顺序获取不同的Page元素,其中Element元素为body

<Pages>
    <Page>1</Page>
    <Page>6</Page>
</Pages>

我似乎得到的最远的是使用以下XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/">
<xsl:for-each select="Objects/Object/Relations/Relation/Placements/Placement[Element='body']">
<xsl:sort select="FrameOrder"/>
    <Page><xsl:value-of select="Page"/></Page>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

这让我:

<Pages>
    <Page>1</Page>
    <Page>1</Page>
    <Page>1</Page>
    <Page>6</Page>
    <Page>6</Page>
</Pages>

此时如何获得Page的不同值?

提前致谢!

1 个答案:

答案 0 :(得分:1)

<xsl:key name="kPage" match="Placement/Page" use="number(.)" />

<xsl:template match="/">
  <Pages>
    <xsl:for-each select="
      //Page[
        generate-id() = generate-id(key('kPage', number(.))[1])
      ]
    ">
      <xsl:sort select="." data-type="number" />
      <xsl:copy-of select="." />
    </xsl:for-each>
  </Pages>
</xsl:template>