带命名空间的xml无法应用xsl

时间:2012-08-07 06:43:27

标签: xml xslt xml-namespaces

我正在尝试将xsl(使用apply-template)应用于xml,

这是xml,

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <SearchResponse xmlns="http://xyz/abcApi">
            <SearchResult>
                <Status>
                    <StatusCode>01</StatusCode>
                    <Description>Successful</Description>
                    <Category>SR</Category>
                </Status>
                <Result>
                    <WSResult>
                        <Index>1</Index>
                        <CityId>111</CityId>
                    </WSResult>
                    <WSResult>
                        <Index>2</Index>
                        <CityId>111</CityId>
                    </WSResult>
                </Result>
                <SessionId>1fc15f22-a670-4f33-b050-c93fa3184cb1</SessionId>
                <IsDomestic>true</IsDomestic>
            </SearchResult>
        </SearchResponse>
    </soap:Body>
</soap:Envelope>

和xsl是,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Response>
            <Param>
                <Ref>
                    <Results>
                        <xsl:apply-templates select="//SearchResponse/SearchResult/Result/WSResult"/>
                    </Results>
                </Ref>
            </Param>
        </Response>
    </xsl:template>
    <xsl:template match="WSResult">
        <Result>
            <Property>
                <xsl:attribute name="Id"><xsl:value-of select="position()"/></xsl:attribute>
                <xsl:attribute name="CityCode"><xsl:value-of select="CityId"/></xsl:attribute>
            </Property>
        </Result>
    </xsl:template>
</xsl:stylesheet>

此xml无法应用上面的指定xsl。请提出解决方案。

1 个答案:

答案 0 :(得分:1)

这是因为您应该在XPath表达式中指定命名空间。 首先在标题中添加名称空间(我选择“搜索”作为前缀,但您可以更改它)。然后将此前缀添加到所有元素名称中:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:search="http://xyz/abcApi">
        <xsl:template match="/">
            <Response>
                <Param>
                    <Ref>
                        <Results>
                            <xsl:apply-templates select="//search:SearchResponse/search:SearchResult/search:Result/search:WSResult"/>
                        </Results>
                    </Ref>
                </Param>
            </Response>
        </xsl:template>
        <xsl:template match="search:WSResult">
            <Result>
                <Property>
                    <xsl:attribute name="Id"><xsl:value-of select="position()"/></xsl:attribute>
                    <xsl:attribute name="CityCode"><xsl:value-of select="search:CityId"/></xsl:attribute>
                </Property>
            </Result>
        </xsl:template>
    </xsl:stylesheet>