XSLT变换字典<字符串,字符串>

问题描述:

我有下面的类:XSLT变换字典<字符串,字符串>

public class HelloWorldDictionary 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public Dictionary<string, string> Items { get; set; } 
} 

而像这样的XSLT:

<xsl:template match="/HelloWorldDictionary"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <br/> 
    <a> 
    Hi there <xsl:value-of select="FirstName" /> <xsl:value-of select="LastName" />! 
    </a> 
    <br/> 
    <br/> 
    <xsl:for-each select="Items/*"> 
    <xsl:value-of select="Key?" /> 
    <span> : </span> 
    <xsl:value-of select="Value?" /> 
    <br/> 
    </xsl:for-each> 
</html> 

正如可以预期,上述的换每个都不行...

生成的XML如下:

<HelloWorldDictionary xmlns="http://schemas.datacontract.org/2004/07/CommunicationTests.XsltEmail" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<FirstName>Foo</FirstName> 
<Items xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
    <a:KeyValueOfstringstring> 
     <a:Key>Key1</a:Key> 
     <a:Value>12345678912</a:Value> 
    </a:KeyValueOfstringstring> 
    <a:KeyValueOfstringstring> 
     <a:Key>Key2</a:Key> 
     <a:Value>ABC1234</a:Value> 
    </a:KeyValueOfstringstring> 
    <a:KeyValueOfstringstring> 
     <a:Key>Key3</a:Key> 
     <a:Value>Test</a:Value> 
    </a:KeyValueOfstringstring> 
</Items> 
<LastName>Bar</LastName> 

什么对我来说是正确的XSLT语法抓住从项目字典中的每个键值对?

+1

你到底该如何将xslt应用到你的班级? – Evk

+2

你的实际XML是什么样的?我假设你在应用样式表之前序列化类。请将XML添加到问题中。 –

+0

对不起,我刚添加它。 – Matei

随着问题中的xml,命名空间是一个尴尬的位;你需要始终观察命名空间。假设您有:

xmlns:dc="http://schemas.datacontract.org/2004/07/CommunicationTests.XsltEmail" 
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 

位于您的xslt顶部;那么我们有(未经测试)类似:

<xsl:template match="/dc:HelloWorldDictionary"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <br/> 
    <a> 
    Hi there <xsl:value-of select="dc:FirstName" /> <xsl:value-of select="dc:LastName" />! 
    </a> 
    <br/> 
    <br/> 
    <xsl:for-each select="dc:Items/*"> 
    <xsl:value-of select="a:Key" /> 
    <span> : </span> 
    <xsl:value-of select="a:Value" /> 
    <br/> 
    </xsl:for-each> 
</html> 

但是,IMO你会更好坚持在大多数情况下,默认的(空)的命名空间;可悲的是,DataContractSerializer不同意...

+0

工作就像一个魅力。只需定义命名空间,使用别名和一切工作,谢谢! – Matei