XSLT转换

时间:2017-04-24 23:24:06

标签: xslt

您好我无法遍历我的输入XML文件以获得所需的输出。请帮助。 期望的输出:

<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport>

我的XSLT文件:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rout="http://safe.com/RoutePlanner/" xmlns:dp="http://www.datapower.com/extensions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" extension-element-prefixes="dp" exclude-result-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/Envelope/Body/ABCCustomMsg/*"/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输入XML:

<Envelope 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/">

<Body> 
<rout:ABCCustomMsg 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
>

<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport></ns1:ABCCustomMsg>
</Body>
</Envelope>

2 个答案:

答案 0 :(得分:0)

您需要在XPath中使用名称空间前缀。这就是他们的目的:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:rout="http://safe.com/RoutePlanner/" 
                xmlns:dp="http://www.datapower.com/extensions" 
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                extension-element-prefixes="dp" exclude-result-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这应该产生输出:

<DocFWImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/"><Header senderID="ABC1234"/></Request></DocFWImport>

如果您确实要从正在复制的元素中删除命名空间(因为您的示例输出没有命名空间),您可以这样做:

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:rout="http://safe.com/RoutePlanner/" 
                xmlns:dp="http://www.datapower.com/extensions" 
                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                extension-element-prefixes="dp" exclude-result-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/>
    </xsl:template>

    <xsl:template match="@*|text()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

这应该会产生您所显示的所需输出。

答案 1 :(得分:0)

您的元素位于命名空间中,但您选择的是无命名空间中的元素。请参阅示例XSLT with XML source that has a default namespace set to xmlns

相关问题