2009-08-20 38 views
1

我有一些非常基本的XML:XSLT新手和XML阵列

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<string>One</string> 
<string>Two</string> 
</ArrayOfString> 

我怎么能翻译成这样:

<ul> 
<li>One</li> 
<li>Two</li> 
</ul> 

使用XSLT?

以前我使用过a:value,但这些只是字符串?

回答

2

我会怎么做:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> 
    <xsl:template match="ArrayOfString"> 
    <ul> 
     <xsl:apply-templates select="string" /> 
    </ul> 
    </xsl:template> 

    <xsl:template match="string"> 
    <li> 
     <xsl:value-of select="." /> 
    </li> 
    </xsl:template> 
</xsl:stylesheet> 
1

是这样的东西你想要什么?

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <ul> 
    <xsl:apply-templates /> 
    </ul> 
</xsl:template> 
<xsl:template match="string"> 
    <li><xsl:value-of select="text()"></xsl:value-of></li> 
</xsl:template> 
</xsl:stylesheet> 

它产生以下输出对我来说:

<?xml version='1.0' ?> 
<ul> 
<li>One</li> 
<li>Two</li> 
</ul> 
+1

您的 “/” 只是作品 “意外” 的模板。您应该使模板显式化,而不是信任隐式默认规则来做正确的事情。只要在样式表的其他地方定义了'ArrayOfString'的实际模板,该解决方案就会中断。 – Tomalak 2009-08-20 11:45:47

+0

够公平的。我刚开始使用我的编辑器提供的基本XSL模板,并添加了东西,直到它工作:) – robertc 2009-08-20 11:57:38

+0

您使用哪种编辑器? – CLiown 2009-08-20 12:29:05