2010-01-18 97 views
2

因此,这可能是一个奇怪的请求,但我会放弃它。我有一个XML文档用于生成XML中指定的html标记的XSLT

<?xml version="1.0" encoding="utf-8" ?> 
<Page> 
    <ID>Site</ID> 
    <Object> 
    <ID>PostCode</ID> 
    <Type>div</Type> 
    <Class>display-label</Class> 
    <Value>PostCode</Value> 
    </Object> 
    <Object> 
    <ID>PostCodeValue</ID> 
    <Type>div</Type> 
    <Class>display-field</Class> 
    <Value>$PostCode</Value> 
    </Object> 
</Page> 

和我使用这个XSL将其改造成一个HTML页面

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     </head> 
     <body> 
     <xsl:for-each select="Page/Object"> 
      &lt;<xsl:value-of select="Type"/>&gt; 
      <xsl:value-of select="Value"/> 
      &lt;/<xsl:value-of select="Type"/>&gt; 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

正如你可以看到我想要生成正确的HTML标签取决于类型节点在xml中。问题是“<”在xsl中不被接受并且编码会阻止它被识别为标记。

任何建议我会怎么做呢?

预先感谢

回答

6

xsl:element使用创建输出文档中的元素节点。

<xsl:element name="{Type}" > 
<xsl:value-of select="Value"/> 
</xsl:element> 

:name属性大括号可能看起来很奇怪,如果你不熟悉。它是一个Attribute Value Template,用于评估属性声明中的XPATH表达式。

应用到你的样式表:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     </head> 
     <body> 
     <xsl:for-each select="Page/Object"> 
      <xsl:element name="{Type}" > 
       <xsl:value-of select="Value"/> 
      </xsl:element> 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 
+0

+1尼斯。在http://www.w3schools.com/xsl/ – Johan 2010-01-18 13:35:49

+0

进行测试时,它的工作原理我将如何使用类值填充此元素 – 2010-01-18 15:29:11

+0

您是否需要为'div'添加类属性?您可以使用''(例如' foo') – 2010-01-18 16:13:50

-1

你需要禁用输出转义像这样来包装你的“<”文本节点:

<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="temp.xml" --> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     </head> 
     <body> 
     <xsl:for-each select="Page/Object"> 
      <xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="Type"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text> 
      <xsl:value-of select="Value"/> 
      <xsl:text disable-output-escaping="yes">&lt;/</xsl:text><xsl:value-of select="Type"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text> 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 
+1

最初发布的XSLT 1.0说:“对于用于除结果树中的文本节点以外的其他内容的文本节点禁用输出转义是错误的。” – 2010-01-18 13:25:16

+0

xsl:元素将是最好的方式来做到这一点,但要回答您需要使用上面的xsl:文本的实际问题。 – Josh 2010-01-18 13:25:43

+0

当我测试它时,它不起作用。 – Johan 2010-01-18 13:36:15