Zakariya Kasu
Zakariya Kasu

Reputation: 13

Name space issues

I need to input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" into the header of a message I need to send to a third party.
Or is there a way to remove the xsi:release attribute but also keep xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance?

This is my xslt:

<xsl:choose>
    <xsl:when test="$msgtype='Nomination_Document'">
        <xsl:element name="Nomination_Document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace="{$msgns}">
            <xsl:attribute name="xsi:release" xmlns="http://www.w3.org/2001/XMLSchema-instance">
            </xsl:attribute> 
            <xsl:attribute name="release" xmlns="http://www.w3.org/2001/XMLSchema-instance">
                <xsl:value-of select="'3'"/>
            </xsl:attribute> 

This is my input:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TSMessage xmlns="http://ECG.NOMSInhouse/V1.1.0.0">
    <Header>
        <MsgNumber Revision="1" MsgNo="840711"/>
        <GenerationDate Date="2019-04-16T11:55:22+02:00"/>
        <MsgSender Role="ZSH" Label="" CodeAgency="" ID=""/>
        <MsgReceiver Role="ZSO" Label="" CodeAgency="" ID=""/>
        <MsgType TypeCode="TN"/>
        <Subject Text=""/>
        <MsgPeriod TimeTo="2019-04-18T06:00:00+02:00" TimeFrom="2019-04-17T06:00:00+02:00"/>
        <Contract ContractID=""/>

This is what I get now:

<?xml version="1.0" encoding="UTF-8"?><Nomination_Document 
 xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:release="" 
 release="3">

I need the header of the message to look like:

<?xml version="1.0" encoding="UTF-8"?><Nomination_Document 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1 " release="3">

Upvotes: 1

Views: 91

Answers (2)

Tomalak
Tomalak

Reputation: 338108

Don't use <xsl:element> and <xsl:attribute> except when the element/attribute names are dynamically calculated. If they are fixed (like in this case), just write down the elements/attributes directly.

This stylesheet:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ecg="http://ECG.NOMSInhouse/V1.1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1"
    exclude-result-prefixes="ecg xsi"
>
    <xsl:output indent="yes" encoding="utf-8" />

    <xsl:template match="ecg:TSMessage[ecg:Header/ecg:MsgType/@TypeCode = 'TN']">
        <Nomination_Document release="3">
            <xsl:comment> ... more processing ... </xsl:comment>
        </Nomination_Document>
    </xsl:template>
</xsl:stylesheet>

Applied to this XML:

<TSMessage xmlns="http://ECG.NOMSInhouse/V1.1.0.0">
    <Header>
        <MsgNumber Revision="1" MsgNo="840711"/>
        <GenerationDate Date="2019-04-16T11:55:22+02:00"/>
        <MsgSender Role="ZSH" Label="" CodeAgency="" ID=""/>
        <MsgReceiver Role="ZSO" Label="" CodeAgency="" ID=""/>
        <MsgType TypeCode="TN"/>
        <Subject Text=""/>
        <MsgPeriod TimeTo="2019-04-18T06:00:00+02:00" TimeFrom="2019-04-17T06:00:00+02:00"/>
        <Contract ContractID=""/>
    </Header>
</TSMessage>

produces this result:

<?xml version="1.0" encoding="utf-8"?>
<Nomination_Document release="3" xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1">
    <!-- ... more processing ... -->
</Nomination_Document>

Notes:

  • I recommend to prefer multiple <xsl:template match="something specific"> over a single <xsl:coose>, simply because your code will be less deeply nested.
  • Declare all namespaces you intend to use at the <xsl:stylesheet> level.
  • You can pick namespace prefixes freely - I chose ecgfor the http://ECG.NOMSInhouse/V1.1.0.0 namespace, you you can pick something you like better.
  • Use exclude-result-prefixes to prevent unneeded namespace declarations from showing up in the output.

Upvotes: 1

Vebbie
Vebbie

Reputation: 1695

You can remove xsi:release and add this line <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>

An example like below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="xml" indent="yes"/>

<xsl:variable name="msgtype" select="'Nomination_Document'" />
<xsl:variable name="msgns" select="'urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1'" />

<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="$msgtype='Nomination_Document'">
            <xsl:element name="Nomination_Document" namespace="{$msgns}">
                <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
                <xsl:attribute name="release">
                    <xsl:value-of select="'3'" />
                </xsl:attribute>
            </xsl:element>
        </xsl:when>
    </xsl:choose>
</xsl:template>

https://xsltfiddle.liberty-development.net/jyRYYiB

Upvotes: 0

Related Questions