none
Copying a node using xslt

    Question

  • Hello,

    I'm trying to use xslt to perform a simple map. 

    First I define a variable pointing to the node which I want to copy and then use "xsl:copy-of select=$var"

    Ex. <xsl:variable name="myVar1" select="//cds/cd[@name='2']/node1/node2/addr2" />

    This is working fine for a simple test xsl and test xml. However with actual xml, it is just not copying the node at all.

    Only difference between test xsl and actual one is that the names of nodes are really long in the actual xsl (xml on which this needs to be applied has long node name), and the test xsl & xml dont have any namespace prefixes.

    It works fine if I point the variable only till root node ie <xsl:variable name="myVar1" select="//cds/cd[@name='2'] but I want to copy the node somewhere below that (ie node1/node2/addr2)

    I dont see any error in xsl (I use XMLSpy), just wondering if there's any limitation on the length of node names while defining variables?

    Thanks,

    Ashish



    • Edited by btsdotnet Saturday, June 14, 2014 11:39 AM
    Saturday, June 14, 2014 8:03 AM

Answers

  • I found out the issue, it was namespace problem.

    This XSLT works

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ans="http://namespace1" xmlns:a="http://namespace2">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
    <xsl:template match="/">
    
    <xsl:variable name="myVar1" select="//cds/cd[@name='1']/ans:node1/ans:node2/a:addr3" />
    
    	<addr>
    	<xsl:copy-of select="$myVar1" />
        </addr>
            
    </xsl:template>
    </xsl:stylesheet>
    
    
    
    

    • Marked as answer by btsdotnet Sunday, June 15, 2014 7:28 AM
    Sunday, June 15, 2014 7:27 AM

All replies

  • XML

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Edited by XMLSpy -->
    <store>
    	<cds>
    	
    	<cd  name="0">
    			<test>this is test 0</test>
    	</cd>
    	
    	<cd  name="1">		
    		<node1 xmlns="http://namespace1">
    			<node2 xmlns:a="http://namespace2">
    				<addr1>
    					<country>country1</country>
    				</addr1>
    				<addr2>
    					<state>state1</state>
    				</addr2>
    				<a:addr3>
    						<city>city1</city>
    						<phone>123</phone>				
    						<email>email1</email>
    						<website xmlns:b="http://namespace3">
    							<b:name>website name</b:name>
    							<a:category>blog</a:category>
    						</website>								
    				</a:addr3>
    			</node2>
    		</node1>
    	</cd>
    	
    	<cd  name="2">
    			<test>this is test2</test>
    	</cd>
    	
    </cds>
    </store>


    XSL

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://namespace2">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
    <xsl:template match="/">
    
    <xsl:variable name="myVar1" select="//cds/cd[@name='1']/node1/node2/a:addr3" />
    
    	<addr>
    	<xsl:copy-of select="$myVar1" />
        </addr>
            
    </xsl:template>
    </xsl:stylesheet>
    
    
    

    Current output = <addr xmlns:a="http://namespace2"/>

    Desired output is 

    <addr xmlns:a="http://namespace2">
    		<a:addr3>
    				<city>city1</city>
    				<phone>123</phone>				
    				<email>email1</email>
    				<website xmlns:b="http://namespace3">
    					<b:name>website name</b:name>
    					<a:category>blog</a:category>
    				</website>								
    		</a:addr3>
    </addr>


    • Edited by btsdotnet Sunday, June 15, 2014 5:08 AM
    Sunday, June 15, 2014 5:01 AM
  • I found out the issue, it was namespace problem.

    This XSLT works

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ans="http://namespace1" xmlns:a="http://namespace2">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
    <xsl:template match="/">
    
    <xsl:variable name="myVar1" select="//cds/cd[@name='1']/ans:node1/ans:node2/a:addr3" />
    
    	<addr>
    	<xsl:copy-of select="$myVar1" />
        </addr>
            
    </xsl:template>
    </xsl:stylesheet>
    
    
    
    

    • Marked as answer by btsdotnet Sunday, June 15, 2014 7:28 AM
    Sunday, June 15, 2014 7:27 AM