2010-01-18 92 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