将数据分组

时间:2017-04-07 10:39:13

标签: xslt-2.0

我想我在这里缺少一些简单的东西。我有一个源XML文件

<Inventory Division="B"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <StackGroup name="Warehouse">
        <Stack>

            <Mainstack name="PRIMARY">
                <MainstackGroup name="GROUP_PRIMARY">
                    <MainstackLayer sequence="1">
                        <StackLayerRef id="LAYER_1"/>
                    </MainstackLayer>
                </MainstackGroup>
            </Mainstack>

            <Mainstack name="SECONDARY">
                <MainstackGroup name="GROUP_SECONDARY">
                    <MainstackLayer sequence="2">
                        <StackLayerRef id="LAYER_2"/>
                    </MainstackLayer>
                </MainstackGroup>
            </Mainstack>

        </Stack>
    </StackGroup>
</Inventory>

我正在应用样式表:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <Mainstack>

            <NumberOfStacks>
                <xsl:value-of select="count(/Inventory/StackGroup/Stack/Mainstack)"/>
            </NumberOfStacks>

            <StackDisplayOrder>
                <xsl:apply-templates select="/Inventory/StackGroup/Stack/Mainstack" mode="order"/>
            </StackDisplayOrder>

            <xsl:apply-templates select="/Inventory/StackGroup/Stack/Mainstack" mode="stacklist"/>

        </Mainstack>
    </xsl:template>

    <xsl:template match="/Inventory/StackGroup/Stack/Mainstack" mode="order">
        <Index><xsl:value-of select="position() - 1" /></Index>
    </xsl:template>

    <xsl:template match="/Inventory/StackGroup/Stack/Mainstack" mode="stacklist">
        <Stack>
            <Index><xsl:value-of select="position() - 1" /></Index>
            <Name>
                <xsl:value-of select="/Inventory/StackGroup/Stack/Mainstack/@name"/>
            </Name>
            <GroupName>
                <xsl:value-of select="/Inventory/StackGroup/Stack/Mainstack/MainstackGroup/@name"/>
            </GroupName>
            <SequenceNo>
                <xsl:value-of select="/Inventory/StackGroup/Stack/Mainstack/MainstackGroup/MainstackLayer/@sequence"/>
            </SequenceNo>
            <LayerNo>
                <xsl:value-of select="/Inventory/StackGroup/Stack/Mainstack/MainstackGroup/MainstackLayer/StackLayerRef/@id"/>
            </LayerNo>
        </Stack>
    </xsl:template>

</xsl:stylesheet>

我得到以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<Mainstack>
   <NumberOfStacks>2</NumberOfStacks>
   <StackDisplayOrder>
      <Index>0</I`enter code here`ndex>
      <Index>1</Index>
   </StackDisplayOrder>
   <Stack>
      <Index>0</Index>
      <Name>PRIMARY SECONDARY</Name>
      <GroupName>GROUP_PRIMARY GROUP_SECONDARY</GroupName>
      <SequenceNo>1 2</SequenceNo>
      <LayerNo>LAYER_1 LAYER_2</LayerNo>
   </Stack>
   <Stack>
      <Index>1</Index>
      <Name>PRIMARY SECONDARY</Name>
      <GroupName>GROUP_PRIMARY GROUP_SECONDARY</GroupName>
      <SequenceNo>1 2</SequenceNo>
      <LayerNo>LAYER_1 LAYER_2</LayerNo>
   </Stack>
</Mainstack>

模板显然找到两个匹配并连接两者的数据。我该如何分开它们?

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用相对路径进行更改,例如

        <Name>
            <xsl:value-of select="/Inventory/StackGroup/Stack/Mainstack/@name"/>
        </Name>

        <Name>
            <xsl:value-of select="@name"/>
        </Name>

依此类推模板中的所有路径。