如何从标题中获取订单项的价值

时间:2011-08-11 13:43:49

标签: xslt xpath

我有这个xml:

<Documents>
    <Header Document_No="Numb1" Warehouse="WH2">
        <Body Position="1" Quantity="11" />
        <Body Position="2" Quantity="10" />
    </Header>
    <Header Document_No="Numb2" Warehouse="WH1">
        <Body Position="1" Quantity="20"/>
        <Body Position="2" Quantity="13"/>
        <Body Position="3" Quantity="12"/>
    </Header>
</Documents>

我的xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output encoding="utf-8" indent="yes" method="xml"/>
    <xsl:template match="/">
        <GroupDocuments>
            <xsl:apply-templates/>
        </GroupDocuments>
    </xsl:template>
    <xsl:template match="Header">
        <Message>
            <Document>
                <Document-Header>
                    <DocNumber>
                        <xsl:value-of select="@Document_No"/>
                    </DocNumber>
                </Document-Header>
                <Lines>
                    <xsl:apply-templates select="Body"/>
                </Lines>
            </Document>
        </Message>
    </xsl:template>
    <xsl:template match="Body">
        <Line>
            <Line-Item>
                <LineNumber>
                    <xsl:value-of select="@Position"/>
                </LineNumber>
                <Quantity>
                    <xsl:value-of select="@Quantity"/>
                </Quantity>
            </Line-Item>
            <Line-Warehouse>
                <Warehouse>
                    <--Where is a problem-->>
                    <xsl:value-of select=""/>
                </Warehouse>
            </Line-Warehouse>
        </Line>
    </xsl:template>

</xsl:stylesheet>

我坚持在Line-Warehouse标签中获取仓库的价值,当我处理Body时如何从标题中获取仓库的价值?请帮忙解决。

结果应为:

<?xml version="1.0" encoding="utf-8"?>
<GroupDocuments xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <Message>
        <Document>
            <Document-Header>
                <DocNumber>Numb1</DocNumber>
            </Document-Header>
            <Lines>
                ..............................................
                ............................................
                <Line>
                    <Line-Item>
                        <LineNumber>2</LineNumber>
                        <Quantity>10</Quantity>
                    </Line-Item>
                    <Line-Warehouse>
                        <Warehouse>WH2</Warehouse>
                    </Line-Warehouse>
                </Line>
            </Lines>
        </Document>
    </Message>
    <Message>
        <Document>
            <Document-Header>
                <DocNumber>Numb2</DocNumber>
            </Document-Header>
            <Lines>
                <Line>
                    <Line-Item>
                        <LineNumber>1</LineNumber>
                        <Quantity>20</Quantity>
                    </Line-Item>
                    <Line-Warehouse>
                        <Warehouse>WH1</Warehouse>
                    </Line-Warehouse>
                </Line>
.....................................
.....................................
            </Lines>
        </Document>
    </Message>
</GroupDocuments>

1 个答案:

答案 0 :(得分:1)

在上述模板中,当前节点是body节点。您想要选择其父级的属性。

使用这个简单的XPath:

"../@Warehouse"