XSLT - 删除前缀命名空间特定节点XML

时间:2016-11-14 12:58:24

标签: xml xslt xml-namespaces prefix

这是带有SOAP标头和正文的XML:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <RequestResponse xmlns="http://tempuri.org/">
            <a:RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" 
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:Message>Message text testing.</a:Message>
                <a:Response>false</a:Response>
            </a:RequestResult>
        </RequestResponse>
    </s:Body>
</s:Envelope>

我需要仅删除RequestResult节点中的前缀。 从这个

 <a:RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" 
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

要:

 <RequestResult xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting" 
            xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

这是我在版本2中使用的XSLT 配置文件

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output omit-xml-declaration="yes" indent="yes" />

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

    <xsl:template match="/">
        <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
            <s:Body>
                <xsl:apply-templates />
            </s:Body>
        </s:Envelope>
    </xsl:template>

    <xsl:template match="RequestResult |RequestResult//*">
        <xsl:element name="a:{name()}"
            namespace="http://schemas.datacontract.org/2004/07/Testing">
            <xsl:namespace name="a"
                select="'http://schemas.datacontract.org/2004/07/MockupTesting'" />
            <xsl:namespace name="i"
                select="'http://www.w3.org/2001/XMLSchema-instance'" />
            <!-- <xsl:copy-of select="namespace::*" /> -->
            <xsl:apply-templates select="node()|@*" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

我应该添加或修改哪些内容以删除该节点上的前缀?

1 个答案:

答案 0 :(得分:0)

你不能删除前缀&#34;来自一个节点。前缀是节点名称的一部分。要删除前缀,您必须创建一个具有其他名称的新节点,并且可能 - 如您的示例中 - 在另一个名称空间中:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://schemas.datacontract.org/2004/07/MockupTesting">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="a:RequestResult">
    <xsl:element name="RequestResult" namespace="http://tempuri.org/">
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>
相关问题