Uploaded image for project: 'XalanJ2'
  1. XalanJ2
  2. XALANJ-2629

Trasformation using Xalan removes multiple namespaces from XML

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Blocker
    • Resolution: Not A Bug
    • None
    • None
    • transformation
    • Security Level: No security risk; visible to anyone (Ordinary problems in Xalan projects. Anybody can view the issue.)
    • None
    • fp1

    Description

      Use below Java code to transform an xml string using xsl

       

      package main;

      import java.io.StringReader;
      import java.io.StringWriter;

      import javax.xml.transform.Transformer;
      import javax.xml.transform.TransformerFactory;
      import javax.xml.transform.stream.StreamResult;
      import javax.xml.transform.stream.StreamSource;
      public class testwsdl

      {     static String xmlIn = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" +          "<AssignMessage async=\"false\" continueOnError=\"false\" enabled=\"true\" name=\"SetWSDL\">\r\n" +          "    <Set>\r\n" +          "        <Payload>\r\n" +          "            <wsdl:definitions xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" targetNamespace=\"urn:sap-com:document:sap:soap:functions:mc-style\" xmlns:n1=\"urn:sap-com:document:sap:rfc:functions\" xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\" xmlns:tns=\"urn:sap-com:document:sap:soap:functions:mc-style\" xmlns:http=\"http://schemas.xmlsoap.org/wsdl/http/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsoap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:mime=\"http://schemas.xmlsoap.org/wsdl/mime/\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">\r\n" +          "            </wsdl:definitions>\r\n" +          "        </Payload>\r\n" +          "    </Set>\r\n" +          "    <AssignVariable>\r\n" +          "        <Name>name</Name>\r\n" +          "        <Value/>\r\n" +          "        <Ref/>\r\n" +          "    </AssignVariable>\r\n" +          "    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>\r\n" +          "    <AssignTo createNew=\"false\" transport=\"http\" type=\"request\"/>\r\n" +          "</AssignMessage>";   static String xsl = "<?xml version=\"1.0\"?>\r\n" +        "<xsl:stylesheet version=\"1.0\"\r\n" +        "  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\r\n" +        "\r\n" +        "  <xsl:output indent=\"yes\" method=\"xml\" encoding=\"utf-8\" />\r\n" +        "\r\n" +        "  <xsl:variable name=\"vTopDefaultNamespace\" select=\"namespace-uri(/)\" />\r\n" +        "\r\n" +        "\r\n" +        "  <xsl:template match=\"node()|@\">\r\n" +        "    <xsl:copy>\r\n" +        "      <xsl:apply-templates select=\"node()|@\" />\r\n" +        "    </xsl:copy>\r\n" +        "  </xsl:template>\r\n" +        "\r\n" +        "  <xsl:template priority=\"10\"\r\n" +        "    match=\"[name() = local-name()\r\n" +        "        and\r\n" +        "          namespace-uri() = namespace-uri(/*)\r\n" +        "         ]\">\r\n" +        "    <xsl:element name=\"

      {name()}

      \">\r\n" + 
            "      <xsl:apply-templates select=\"node()|@*\" />\r\n" + 
            "    </xsl:element>\r\n" + 
            "  </xsl:template>\r\n" + 
            "\r\n" + 
            "  <xsl:template match=\"*\" priority=\"5\">\r\n" + 
            "    <xsl:variable name=\"velemNSName\" select=\"substring-before(name(), ':')\" />\r\n" + 
            "\r\n" + 
            "    <xsl:element name=\"{name()}\" namespace=\"{namespace-uri()}\">\r\n" + 
            "      <xsl:copy-of select=\"namespace::*[name() and not(name() = $velemNSName)]\" />\r\n" + 
            "      <xsl:apply-templates select=\"node()|@*\" />\r\n" + 
            "    </xsl:element>\r\n" + 
            "  </xsl:template>\r\n" + 
            "  <xsl:template match=\"comment()\" />\r\n" + 
            "</xsl:stylesheet>";
          public static void main(String[] args) throws Exception

      {         StreamSource xslSource = new StreamSource(new StringReader(xsl));         StreamSource xmlInSource = new StreamSource(new StringReader(xmlIn));         Transformer tf = TransformerFactory.newInstance().newTransformer(xslSource);         StringWriter xmlOutWriter = new StringWriter();         tf.transform(xmlInSource, new StreamResult(xmlOutWriter));         System.out.println(xmlOutWriter.toString());     }

      }

       

      Expected output is:

      <?xml version="1.0" encoding="utf-8"?><AssignMessage async="false" continueOnError="false" enabled="true" name="SetWSDL">
          <Set>
              <Payload>
                  <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:n1="urn:sap-com:document:sap:rfc:functions" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="urn:sap-com:document:sap:soap:functions:mc-style" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsoap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" targetNamespace="urn:sap-com:document:sap:soap:functions:mc-style">
                  </wsdl:definitions>
              </Payload>
          </Set>
          <AssignVariable>
              <Name>name</Name>
              <Value/>
              <Ref/>
          </AssignVariable>
          <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
          <AssignTo createNew="false" transport="http" type="request"/>
      </AssignMessage>

       

      It works when Trasformer from default library from JDK is used (com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl)

       

      Output when maven xalan library with any version is used (org.apache.xalan.transformer.TransformerImpl):

      <?xml version="1.0" encoding="utf-8"?><AssignMessage async="false" continueOnError="false" enabled="true" name="SetWSDL">
          <Set>
              <Payload>
                  <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:sap-com:document:sap:soap:functions:mc-style">
                  </wsdl:definitions>
              </Payload>
          </Set>
          <AssignVariable>
              <Name>name</Name>
              <Value/>
              <Ref/>
          </AssignVariable>
          <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
          <AssignTo createNew="false" transport="http" type="request"/>
      </AssignMessage>

       

      You can observe that multiple namespaces are removed.

      Attachments

        Activity

          People

            ggregory Gary D. Gregory
            LavanyaHegde Lavanya Hegde
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: