ohadinho
ohadinho

Reputation: 7144

How to write xsl that will map <xsl:text></xsl:text>

I am using two different xslt processors. One of them creates an element which is not empty in the target (even that this element is empty in the source).

To get to the point, how can I tell the processor via my xsl that the element should be created in target is empty? (in other words, how to map to that element).

EDITED: To clearify my problem - 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>

Upvotes: 1

Views: 1525

Answers (2)

Rookie Programmer Aravind
Rookie Programmer Aravind

Reputation: 12154

That may be because your input XML element has a space in it .. Usage of strip-space should get ur job done!

This rips-off the space between nodes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="YourElement" />
<!--Your code here-->

Input XML:

<a>
  <b> </b>
<a>

Output XML:

<a>
  <b/>
</a>

This preserves space between nodes :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="YourElement" />
<!--Your code here-->

If you want to apply same rule for all elements then

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="*" />

source XML:

<a>
  <b> </b>
</a>

Output XML:

<a>
  <b> </b>
<a>

Upvotes: 2

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

The following transformation demonstrates two different ways of creating empty elements:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <t>
       <a>
         <b/>
       </a>
       <a>
         <xsl:element name="b"/>
       </a>
     </t>
 </xsl:template>
</xsl:stylesheet>

The result with 7 different XSLT (MSXML3. MSXML4, MSXML6, .NET XslCompiledTransform, .NET XslTransform, Saxon 6.5.4 and AltovaXML (XML-SPY) processors contains two separate <b/> elements:

<t>
   <a>
      <b/>
   </a>
   <a>
      <b/>
   </a>
</t>

Upvotes: 1

Related Questions