具有XML命名空间的Xsl [英] Xsl with XML namespace
问题描述
当xml具有命名空间时,我如何在xml节点上选择。
这里没有命名空间的简单xml。
How i select on xml node when xml have namespace.
Here simple xml without namespace.
<?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>
我的xsl是:
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>
C#转换
Tranformation with C#
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("xslTest.xsl");
xslt.Transform("Ser.xml", "Result.txt");
我的结果是:
my result is:
<?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>
一切顺利且有效。
现在我序列化datacontract我的班级因为我现在有一个类的字典。
我的xml现在是:
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 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>
如何修改我的xsl以进行正确的转换?
如果键Sinn我如何获得价值?
我尝试了什么:
直接使用xsl - xml的唯一节点:
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 version="1.0" encoding="utf-8"?><Root><List><HouseListName><Name></Name><Rooms /></HouseListName></List></Root>
我试试以下网站:
XSLT转换XML w命名空间 - 堆栈溢出 [ ^ ]
i试试这个
I have try following site:
XSLT Transform XML with Namespaces - Stack Overflow[^]
i try this
<?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得到这个
i get this
<?xml version="1.0" encoding="utf-8"?>88HomeSinn42Egal8821Kitchen42Test
而没有d匹配 - 我的意思是匹配=/与d命名空间
i得到这个
and without d on match - i mean match="/" with d namespace
i get this
<?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读这个,但这甚至不起作用
xslt - 如何从带名称空间的XML中选择? - 堆栈溢出 [ ^ ]
我能做什么?有人为我链接或帮助吗?
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?
推荐答案
我有解决方案。 我有解决方案 [ ^ ]很好。你必须在每个节点上添加前缀。
喜欢这个:
I have solution. The tip of I have solution[^] was good. You must on every node add prefix.
Like in this:
<?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:
Result is:
<?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?
Solved! =)
exclude-result-prefixes remove namespace from the xsl in the new xml output file
How I make it the question as solved?
这篇关于具有XML命名空间的Xsl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!