Map empty to a node with xslt

1.8k Views Asked by At

I am using "Altova MapForce" mapping tool and I'm mapping node to node. The resulted xml (Using the altova xslt processor) is without nodes that are not empty. The resulted xml (Using the biztalk xslt processor, with the generated xsl from the Altova Mapforce) is with nodes that are not empty. That nodes are empty on the source.

My Goal is to connect the source node (Using Altova MapForce mapping tool) to a custom written XSLT, and then to connect it to the target.

Here is my code: in.xml - Instance input for executing the map (Please notice the empty id tag: )

<ns0:Root xmlns:ns0="http://BizTalk_Server_Project1.Schema1">
  <id root="root_0" extension="extension_1" />
</ns0:Root>

Schema1.xsd - source and target schema

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://BizTalk_Server_Project1.Schema1" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://BizTalk_Server_Project1.Schema1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id">
          <xs:complexType>
            <xs:attribute name="root" type="xs:string" />
            <xs:attribute name="extension" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

New.mfd - Altova mapping file

<?xml version="1.0" encoding="UTF-8"?>
<mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="19">
    <component name="defaultmap1" blackbox="0" uid="1" editable="1">
        <properties SelectedLanguage="xslt"/>
        <structure>
            <children>
                <component name="document" library="xml" uid="4" kind="14">
                    <properties/>
                    <view rbx="150" rby="200"/>
                    <data>
                        <root>
                            <header>
                                <namespaces>
                                    <namespace/>
                                    <namespace uid="http://BizTalk_Server_Project1.Schema1"/>
                                    <namespace uid="http://www.altova.com/mapforce"/>
                                </namespaces>
                            </header>
                            <entry name="FileInstance" ns="2" expanded="1">
                                <entry name="document" ns="2" expanded="1" casttotargettypemode="cast-in-subtree">
                                    <entry name="Root" ns="1" expanded="1">
                                        <entry name="id" expanded="1">
                                            <entry name="extension" type="attribute" outkey="4"/>
                                        </entry>
                                    </entry>
                                </entry>
                            </entry>
                        </root>
                        <document schema="Schema1.xsd" outputinstance="Schema1.xml" instanceroot="{http://BizTalk_Server_Project1.Schema1}Root"/>
                        <wsdl/>
                    </data>
                </component>
                <component name="document" library="xml" uid="5" kind="14">
                    <properties XSLTDefaultOutput="1"/>
                    <view ltx="593" rbx="743" rby="200"/>
                    <data>
                        <root>
                            <header>
                                <namespaces>
                                    <namespace/>
                                    <namespace uid="http://BizTalk_Server_Project1.Schema1"/>
                                    <namespace uid="http://www.altova.com/mapforce"/>
                                </namespaces>
                            </header>
                            <entry name="FileInstance" ns="2" expanded="1">
                                <entry name="document" ns="2" expanded="1" casttotargettypemode="cast-in-subtree">
                                    <entry name="Root" ns="1" expanded="1">
                                        <entry name="id" expanded="1">
                                            <entry name="extension" type="attribute" inpkey="5"/>
                                        </entry>
                                    </entry>
                                </entry>
                            </entry>
                        </root>
                        <document schema="Schema1.xsd" outputinstance="Schema1.xml" instanceroot="{http://BizTalk_Server_Project1.Schema1}Root"/>
                        <wsdl/>
                    </data>
                </component>
            </children>
            <graph directed="1">
                <edges/>
                <vertices>
                    <vertex vertexkey="4">
                        <edges>
                            <edge vertexkey="5" edgekey="6"/>
                        </edges>
                    </vertex>
                </vertices>
            </graph>
        </structure>
    </component>
</mapping>

Xsl.xsl - Xsl generated by the Altova Mapper

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://BizTalk_Server_Project1.Schema1" xmlns:agt="http://www.altova.com/Mapforce/agt" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="ns0 agt xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template name="agt:var2_MapToSchema1_function">
        <xsl:param name="par0"/>
        <xsl:attribute name="extension">
            <xsl:value-of select="string($par0/@extension)"/>
        </xsl:attribute>
    </xsl:template>
    <xsl:template match="/">
        <Root xmlns="http://BizTalk_Server_Project1.Schema1">
            <xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance">http://BizTalk_Server_Project1.Schema1 C:/Users/OhadAv/Desktop/ForAltova/Schema1.xsd</xsl:attribute>
            <id xmlns="">
                <xsl:for-each select="ns0:Root/id">
                    <xsl:variable name="var1_extension">
                        <xsl:if test="@extension">
                            <xsl:value-of select="'1'"/>
                        </xsl:if>
                    </xsl:variable>
                    <xsl:if test="string(boolean(string($var1_extension))) != 'false'">
                        <xsl:call-template name="agt:var2_MapToSchema1_function">
                            <xsl:with-param name="par0" select="."/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:for-each>
            </id>
        </Root>
    </xsl:template>
</xsl:stylesheet>

GeneratedByBizTalkMapperAfterXSLTmap.xml - Output instance of the BizTalk mapper after using the "Xsl.xsl" for the map (Please notice the non-empty id tag that has been generated when editing the xml file:

<?xml version="1.0" encoding="utf-8"?>
<Root xsi:schemaLocation="http://BizTalk_Server_Project1.Schema1 C:/Users/OhadAv/Desktop/ForAltova/Schema1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://BizTalk_Server_Project1.Schema1">
  <id extension="extension_1" xmlns="">
  </id>
</Root>
1

There are 1 best solutions below

0
On

The xslt generated by the Altova mapper looks pretty messy. e.g. use of a one time only variable, a call template just used once, and repeated conversion like string(boolean(string(

I'm not 100% clear on what mapping you need to do in your map.

If you want to remove the root attribute entirely, and provide an id element even if it is missing, then the below xslt will work:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:ns0="http://BizTalk_Server_Project1.Schema1" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                exclude-result-prefixes="ns0 xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/ns0:Root">
        <Root xmlns="http://BizTalk_Server_Project1.Schema1">
            <xsl:element name="id">
                <xsl:for-each select="ns0:Root/id">
                    <xsl:if test="@extension">
                        <xsl:attribute name="extension">
                            <xsl:value-of select="string(@extension)"/>
                        </xsl:attribute>
                    </xsl:if>
                </xsl:for-each>
            </xsl:element>
        </Root>
    </xsl:template>
</xsl:stylesheet>

This produces XML without the xmlns, extraneous schema locations, etc like so:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://BizTalk_Server_Project1.Schema1">
  <id extension="extension_1" />
</Root>

If however you mean that you want to omit the id element entirely if it isn't present on the input node, then change the template as follows.

<xsl:template match="/ns0:Root">
    <Root xmlns="http://BizTalk_Server_Project1.Schema1">
        <xsl:if test="id">
            <xsl:element name="id">
                <xsl:attribute name="extension">
                    <xsl:value-of select="string(id/@extension)"/>
                </xsl:attribute>
            </xsl:element>
        </xsl:if>
    </Root>
</xsl:template>

Which Maps

<ns0:Root xmlns:ns0="http://BizTalk_Server_Project1.Schema1">
</ns0:Root>

To:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://BizTalk_Server_Project1.Schema1" />