Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How i select on xml node when xml have namespace.

Here simple xml without namespace.
XML
<?xml version="1.0" encoding="utf-8"?>
<HouseList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>Test</Name>
  <Id>42</Id>
  <HList>
    <House>
      <Name>Home</Name>
      <Id>88</Id>
      <RoomList>
        <Room>
          <Name>Kitchen</Name>
          <Id>21</Id>
        </Room>
      </RoomList>
    </House>
  </HList>
</HouseList>


my xsl is:
<pre lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
 <Root>
	<List>
		<HouseListName> 
			<Name>
				<xsl:value-of select="HouseList/Name"/>
			</Name>
			<Rooms>
				<xsl:for-each select="HouseList/HList">
					<Ho>				
						<Name>
							<xsl:value-of select="House/Name"/>
						</Name>
						<R>
							<xsl:for-each select="House/RoomList">
								<Room>				
									<Name>
										<xsl:value-of select="Room/Name"/>	
									</Name>
								</Room>	
							</xsl:for-each>
						</R>					
					</Ho>					
				</xsl:for-each>			
			</Rooms>
		</HouseListName>
	</List>
</Root>
</xsl:template>
</xsl:stylesheet> 


Tranformation with C#

C#
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("xslTest.xsl");
xslt.Transform("Ser.xml", "Result.txt");


my result is:

XML
<?xml version="1.0" encoding="utf-8"?><Root><List><HouseListName><Name>Test</Name><Rooms><Ho><Name>Home</Name><R><Room><Name>Kitchen</Name></Room></R></Ho></Rooms></HouseListName></List></Root>


all good and works.
Now i serialize datacontract my class cause i have now a dictionary in one of a class.

My xml is now:

XML
<?xml version="1.0" encoding="utf-8"?><HouseList xmlns="http://schemas.datacontract.org/2004/07/Xsl_Transformer.Data" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><HList><House><Id>88</Id><Name>Home</Name><Note xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:KeyValueOfstringstring><a:Key>Sinn</a:Key><a:Value>42</a:Value></a:KeyValueOfstringstring><a:KeyValueOfstringstring><a:Key>Egal</a:Key><a:Value>88</a:Value></a:KeyValueOfstringstring></Note><RoomList><Room><Id>21</Id><Name>Kitchen</Name></Room></RoomList></House></HList><Id>42</Id><Name>Test</Name></HouseList>


How i must modify my xsl for correct transformation?
How i get access to value if key "Sinn"?

What I have tried:

direct using of xsl - only node of xml:

XML
<?xml version="1.0" encoding="utf-8"?><Root><List><HouseListName><Name></Name><Rooms /></HouseListName></List></Root>


I have try following site:
XSLT Transform XML with Namespaces - Stack Overflow[^]

i try this
XML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:d="http://schemas.datacontract.org/2004/07/Xsl_Transformer.Data"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/d">
 <Root>
	<List>
		<HouseListName> 
			<Name>
				<xsl:value-of select="d:HouseList/Name"/>
			</Name>
			<Rooms>
				<xsl:for-each select="d:HouseList/HList">
					<Ho>				
						<Name>
							<xsl:value-of select="d:House/Name"/>
						</Name>
						<R>
							<xsl:for-each select="d:House/RoomList">
								<Room>				
									<Name>
										<xsl:value-of select="d:Room/Name"/>	
									</Name>
								</Room>	
							</xsl:for-each>
						</R>					
					</Ho>					
				</xsl:for-each>			
			</Rooms>
		</HouseListName>
	</List>
</Root>
</xsl:template>
</xsl:stylesheet> 

i get this
XML
<?xml version="1.0" encoding="utf-8"?>88HomeSinn42Egal8821Kitchen42Test


and without d on match - i mean match="/" with d namespace
i get this
XML
<?xml version="1.0" encoding="utf-8"?><Root xmlns:d="http://schemas.datacontract.org/2004/07/Xsl_Transformer.Data"><List><HouseListName><Name></Name><Rooms /></HouseListName></List></Root>


i read this, but this even not work
xslt - How to 'select' from XML with namespaces? - Stack Overflow[^]

What i can do? Have someone link or help for me?
Posted
Updated 24-Jul-17 7:33am
v2
Comments
Richard MacCutchan 23-Jul-17 12:43pm    
If you fond some code on StackOverflow that does not work, then you should post your question in that website.
Atlapure Ambrish 24-Jul-17 1:45am    
Try an online tool like this one to tweak your XSL quickly and see the result immediately. http://www.utilities-online.info/xsltransformation/#.WXWBh4SGPcs

1 solution

I have solution. The tip of I have solution[^] was good. You must on every node add prefix.

Like in this:

XML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:d="http://schemas.datacontract.org/2004/07/Xsl_Transformer.Data"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="d">
 
<xsl:template match="/d:HouseList">
 <Root>
	<List>
		<HouseListName> 
			<Name>
				<xsl:value-of select="d:Name"/>
			</Name>
			<Rooms>
				<xsl:for-each select="d:HList">
					<Ho>				
						<Name>
							<xsl:value-of select="d:House/d:Name"/>
						</Name>
						<R>
							<xsl:for-each select="d:House/d:RoomList">
								<Room>				
									<Name>
										<xsl:value-of select="d:Room/d:Name"/>	
									</Name>
								</Room>	
							</xsl:for-each>
						</R>					
					</Ho>					
				</xsl:for-each>			
			</Rooms>
		</HouseListName>
	</List>
</Root>
</xsl:template>
</xsl:stylesheet> 


Result is:

XML
<?xml version="1.0" encoding="UTF-8"?><Root><List><HouseListName><Name>Test</Name><Rooms><Ho><Name>Home</Name><R><Room><Name>Kitchen</Name></Room></R></Ho></Rooms></HouseListName></List></Root>


Solved! =)

exclude-result-prefixes remove namespace from the xsl in the new xml output file

How I make it the question as solved?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900