通过不同的XPath从XML中检索属性值

时间:2014-12-14 10:36:54

标签: xslt xslt-1.0

我有以下xml给我,以及我在XSLT中传递的xml用于转换目的..

<Report xmlns:fpml="http://www.fpml.org/FpML-5/confirmation" 
    xmlns="http://www.eurexchange.com/EurexIRSFullInventoryReport" 
    name="CB202 Full Inventory Report">
    <reportNameGrp>
        <CM>
            <acctTypGrp name="A4">
                <ProductType name="Swap">
                    <currTypCod value="EUR">
                        <rateIndex name="EURIBOR">
                            <rateIndexTenor name="6M">
                                <idxSource>EURIBOR01</idxSource>
                            </rateIndexTenor>
                        </rateIndex>
                    </currTypCod>
                    <currTypCod value="GBP">
                        <rateIndex name="LIBOR">
                            <rateIndexTenor name="1Y">
                                <idxSource>LIBOR01</idxSource>
                            </rateIndexTenor>
                        </rateIndex>
                    </currTypCod>
                </ProductType>
            </acctTypGrp>
        </CM>
    </reportNameGrp>
</Report>

我为此开发了以下XSLT,我稍后将用于转换目的。在下面的XSLT中,我试图从上面的XML中检索值:

<xsl:stylesheet version="1.0" xmlns:fpml="http://www.fpml.org/FpML-5/confirmation" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:eur="http://www.eurexchange.com/EurexIRSFullInventoryReport">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
    <xsl:template match="/eur:Report">
        <Eurexflows>
            <xsl:apply-templates select="eur:reportNameGrp/eur:CM/eur:acctTypGrp/eur:ProductType/eur:currTypCod/eur:rateIndex/eur:rateIndexTenor" />
        </Eurexflows>
    </xsl:template>
    <xsl:template match="eur:rateIndexTenor">
        <EurexMessageObject>
            <CCPTradeId><xsl:value-of select="eur:CCPTradeId/@id" /></CCPTradeId>
            <novDateTime><xsl:value-of select="eur:CCPTradeId/eur:novDateTime" /></novDateTime>
            <feename><xsl:value-of select="eur:CCPTradeId/eur:feeType/@name"/></feename>
            <feePayAmnt><xsl:value-of select="eur:CCPTradeId/eur:feeType/eur:feePayAmnt"/></feePayAmnt>
            <feeCurrTypCod><xsl:value-of select="eur:CCPTradeId/eur:feeType/eur:feeCurrTypCod"/></feeCurrTypCod>
            <feeDate><xsl:value-of select="eur:CCPTradeId/eur:feeType/eur:feeDate"/></feeDate>
            <idxSource><xsl:value-of select="eur:idxSource"/></idxSource>
            <rateIndexTenorname><xsl:value-of select="@name"/></rateIndexTenorname>
            <rateIndexname>
                <!--<xsl:value-of select="eur:report/eur:reportNameGrp/eur:CM/eur:acctTypGrp/@name/eur:ProductType/@name"/>-->
                <xsl:for-each select="eur:report/eur:reportNameGrp/eur:CM/eur:acctTypGrp/eur:ProductType/eur:currTypCod">
                    <xsl:value-of select="eur:rateIndex/@name" />
                </xsl:for-each>
            </rateIndexname>
        </EurexMessageObject>
    </xsl:template>
</xsl:stylesheet>

现在您可以看到,在上面的XSL中,我想要检索属性<rateIndex name>的值,这个值不是理想的。 <rateIndexname>的值应为EURIBORLIBOR

请告知我如何检索name元素上rateIndex属性的值。请解释我的XSLT中应用的XPath出了什么问题。

1 个答案:

答案 0 :(得分:0)

在您的模板中,您当前位于rateIndexTenor元素上。您希望获取名称的rateIndex是其父级。所以,您可以使用此表达式来替换此处不必要的xsl:for-each,因为只有一个父级:

<xsl:value-of select="parent::eur:rateIndex/@name"/>

这可以简化为此,其中..是父节点的简写。

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