[ 
https://issues.apache.org/jira/browse/XALANJ-2629?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17732670#comment-17732670
 ] 

Joe Kesselman commented on XALANJ-2629:
---------------------------------------

{*}NOTE{*}: The version of Xalan that shipped with Sun's Java processors was 
badly backlevel, and in this case I believe it is, in fact, in error.

XSLT 1.0 had the problem that it had no way to match templates against 
namespace nodes, and thus the default identity transformation published in the 
spec:
{code:java}
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>{code}
did not preserve them where they occurred. Instead, the system creates new 
namespace nodes where demanded in the output document. In our code, that's 
implemented in the serializer.

Alas, this is a problem when transforming documents that (like XSLT itself!) 
may reference namespace prefixes within strings and expect them to be 
interpreted by context. Since this user embeds the Identity Transform in their 
stylesheet

The *workaround* is to use the namespace:: axis to explicitly preserve these:
     
{code:java}
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:copy-of select="namespace::*"/> <!-- Added -->
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>{code}

One can quibble about whether the XSLT 1.0 Working Group should have published 
this rather than the simpler version above, but they didn't.

I've tested this against the "expected output" and namespace nodes were 
preserved:


{code:java}
<?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>{code}

So I'm afraid the answer is that [~LavanyaHegde]  needs to modify the 
stylesheet, replacing its version of the default template with one that 
includes this additional clause.

Or to move to an XSLT 2.0 compatable processor, where this was addressed in 
https://www.w3.org/TR/xslt20/#copying:
{quote}The [{{xsl:copy}}|https://www.w3.org/TR/xslt20/#element-copy] 
instruction has an optional {{inherit-namespaces}} attribute, with the value 
{{yes}} or {{{}no{}}}. The default value is {{{}yes{}}}. 
{quote}
*Move to close, working as designed, explanation and solution provided.*

> Trasformation using Xalan removes multiple namespaces from XML 
> ---------------------------------------------------------------
>
>                 Key: XALANJ-2629
>                 URL: https://issues.apache.org/jira/browse/XALANJ-2629
>             Project: XalanJ2
>          Issue Type: Bug
>      Security Level: No security risk; visible to anyone(Ordinary problems in 
> Xalan projects.  Anybody can view the issue.) 
>          Components: transformation
>            Reporter: Lavanya Hegde
>            Assignee: Gary D. Gregory
>            Priority: Blocker
>
> 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.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@xalan.apache.org
For additional commands, e-mail: dev-h...@xalan.apache.org

Reply via email to