简体   繁体   中英

XSLT1.0 :: I want to add a new element in XML with suffix and namespace as its parent

Solution Required only in XSLT1.0. I want to create a single xslt which will work add an element irrespective of namespace at fixed position. I want to add sas_api_key element as seen in the output of both the XMLs

Input of XML 1:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body xmlns:get="http://tempuri.org/getScreeningProfile">
        <get:getScreeningProfile>
            <get:parameters>
                <get:party_number>334857</get:party_number>
            </get:parameters>
        </get:getScreeningProfile>
    </soap:Body>
</soap:Envelope>

Output of XML 1:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body xmlns:get="http://tempuri.org/getScreeningProfile">
        <get:getScreeningProfile>
            <get:parameters>
                <get:sas_api_key>123<get:sas_api_key>
                <get:party_number>334857</get:party_number>
            </get:parameters>
        </get:getScreeningProfile>
    </soap:Body>
</soap:Envelope>

Input of XML2:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:biw="http://www.sas.com/xml/namespace/biwebservices">
    <soapenv:Header/>
    <soapenv:Body>
        <biw:getScreeningProfile_v2>
            <biw:parameters>
                <biw:sas_api_key>123<biw:sas_api_key>
                <biw:party_number>12345</biw:party_number>
            </biw:parameters>
        </biw:getScreeningProfile_v2>
    </soapenv:Body>
</soapenv:Envelope>

Output of XML2:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:biw="http://www.sas.com/xml/namespace/biwebservices">
    <soapenv:Header/>
    <soapenv:Body>
        <biw:getScreeningProfile_v2>
            <biw:parameters>
                <biw:sas_api_key>123<biw:sas_api_key>
                <biw:party_number>12345</biw:party_number>
            </biw:parameters>
        </biw:getScreeningProfile_v2>
    </soapenv:Body>
</soapenv:Envelope>

Below is the XSLT which I have created but its not working as expected. Please assist.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="//*[local-name()='parameters']/*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//*[local-name()='parameters']/*">
        <xsl:element name="sas_api_key" inherit-namespaces="yes">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

In XSLT 1.0, you could do:

<xsl:template match="*[local-name()='parameters']">
    <xsl:copy>
        <xsl:element name="{substring-before(name(), ':')}:sas_api_key" namespace="{namespace-uri()}">123</xsl:element>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

Note that this assumes that the parameters element does have a prefix.


If you have a list of allowed prefixes and namespaces that the parameters element can use, it would be much better to use it, instead of relying on it being the only element with this local name. After all, the very purpose of using a namespace is to distinguish between elements having the same local name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM