2013-03-18 35 views
2

嘿,我剛開始學習xslt我遇到了問題。 我有xml文件,如:Xslt整理所有數據

<projects> 
<project nr="1"> 
<description>z</description> 
<description>a</description> 
</project> 
</projects> 

有這樣的很少描述幾個項目,而且我想要做的就是創建一個HTML表格與所有項目的所有排序的說明。總體而言,如果我有5個項目和2個描述,則會有10個排序行。到目前爲止,我只管理了所有項目的第一個描述,不知道如何包括第二個,如果有第三個第四個等。 任何線索?感謝幫助。

@edit 到目前爲止,我已經開始使用平面文件,但沒關係。還有現在我有

<xsl:output method="text"/> 
<xsl:template match="projects"> 
<xsl:apply-templates> 
<xsl:sort select="description" /> 
</xsl:apply-templates> 
</xsl:template> 
<xsl:template match="project"> 
Description: <xsl:apply-templates select="description"/> 
<xsl:text> 
</xsl:text> 
</xsl:template> 

我一直在瞎搞的:但每種循環我不知道這個應該怎麼做,說實話

+0

你可以發佈你到目前爲止部分工作的xsl嗎?你試圖包括多個項目? – uptownnickbrown 2013-03-18 00:08:35

+0

您是否想對與項目無關的所有描述進行排序,或者是否希望按多個鍵排序的項目(即描述1,然後如果兩個項目的描述1與描述2相同,等等)? – JohnLBevan 2013-03-18 01:14:50

回答

0

這種轉變:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/*"> 
    <xsl:copy> 
    <xsl:apply-templates select="*/description"> 
    <xsl:sort/> 
    </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 
當在下面的XML文檔施加(基於所提供的一個和擴展)

<projects> 
    <project nr="1"> 
     <description>z</description> 
     <description>a</description> 
    </project> 
    <project nr="2"> 
     <description>b</description> 
     <description>y</description> 
    </project> 
    <project nr="3"> 
     <description>c</description> 
     <description>x</description> 
    </project> 
    <project nr="4"> 
     <description>d</description> 
     <description>w</description> 
    </project> 
    <project nr="5"> 
     <description>e</description> 
     <description>u</description> 
    </project> 
</projects> 

產生通緝的結果

<projects> 
    <description>a</description> 
    <description>b</description> 
    <description>c</description> 
    <description>d</description> 
    <description>e</description> 
    <description>u</description> 
    <description>w</description> 
    <description>x</description> 
    <description>y</description> 
    <description>z</description> 
</projects> 
+0

感謝它的工作,但行已「連接」,我無法設法在新行中獲得每一個描述,我不太確定這是如何工作的。 – user2141889 2013-03-19 12:04:24

+0

@ user2141889,不同的XSLT處理器以不同的方式處理空白保存。正如你從這個答案中看到的,我將每個'description'元素都放在一個單獨的行上。這是由於'指令。您的代碼很可能未使用此指令。 – 2013-03-19 14:29:12

0

你是對的,你要使用for-each循環,這樣的事情:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <xsl:for-each select="//project/description"> 
     <xsl:sort select="." /> 
     <xsl:copy-of select="."/> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

這將返回所有的所有項目的<description>節點,按升序排序。如果您想要返回它們的非xml表示形式,則可以用Description: <xsl:value-of select="."/>或任何您喜歡的東西替換<xsl:copy-of select="."/>節點。

+0

不太確定,但是這個解決方案對我不起作用; c – user2141889 2013-03-19 12:09:20