XSLT to remove namespace and add attribute to a ta

2019-08-30 20:42发布

问题:

I want to remove namespaces from the xml and add an attribute to a tag. How it can be achieved by xslt. Here is the input xml:

    <Customers Version="2-0" Type="CustomerInformation" Revision="102" xsi:schemaLocation="http://www.example.com/2011/CustomerInformation-2-0 main-2-0.xsd" xsi:type="CustomerInformationMessage" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.com/2011/CustomerInformation">
    <header>
    <messageId>ABC</messageId>
        <creationTimestamp>2015-04-22T11:40:42-05:00</creationTimestamp>
    </header>
    <CustomerInformation>
        <header>
            <custType>New</custType>
        </header>
        <Customer>
            <Name>Mat</Name>
            <Address>Vegas</Address>
        </Customer>
    </CustomerInformation>
    <CustomerInformation>
        <header>
            <custType>Update</custType>
        </header>
        <Customer>
            <Name>Gina</Name>
            <Address>New York</Address>
        </Customer>
    </CustomerInformation>
</Customers>

Desired output should be:

<Customers Version="2-0" Type="CustomerInformation" Revision="102">
    <header id='ABC'>
    <messageId>ABC</messageId>
        <creationTimestamp>2015-04-22T11:40:42-05:00</creationTimestamp>
    </header>
    <CustomerInformation id='ABC'>
        <header>
            <custType>New</custType>
        </header>
        <Customer>
            <Name>Mat</Name>
            <Address>Vegas</Address>
        </Customer>
    </CustomerInformation>
    <CustomerInformation id='ABC'>
        <header>
            <custType>Update</custType>
        </header>
        <Customer>
            <Name>Gina</Name>
            <Address>New York</Address>
        </Customer>
    </CustomerInformation>
</Customers>

Can someone tell me how to achieve the desired output through xslt version 1.0

回答1:

Your input xml's namespace is http://www.example.com/2011/CustomerInformation, so all the children will inherit that namespace. To get the desired output you want, either remove the namespace from the root of your input file, or give it a new namespace.

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>  
    <xsl:template match="CustomerInformation">
        <xsl:element name="CustomerInformation" namespace="http://www.example.com/2011/CustomerInformation">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="header">
        <xsl:element name="header" namespace="http://www.example.com/2011/CustomerInformation">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="/">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="text() | processing-instruction() | comment()">
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>


标签: xml xslt