2012-03-09 76 views
0

我有以下XML結構駐留在命名空間內:XSLT的,每個節點

<School> 
    <SchoolInfo> 
    <SchoolName>The Big School</SchoolName> 
    <Opened>2008</Opened> 
    <SchoolID>SCH1122</SchoolID> 
    <Geograpics> 
     <Location>London</Location> 
     <PostCode>ZZ11 1ZZ</PostCode> 
    </Geographics> 
    </SchoolInfo> 
    <Pupil> 
    <Name>Tom</Name> 
    <LastName>Jones</LastName> 
    <Class>12B</Class> 
    <Age>16</Age> 
    </Pupil> 
    <Pupil> 
    <Name>Steve</Name> 
    <LastName>Jobs</LastName> 
    <Class>09A</Class> 
    <Age>17</Age> 
    </Pupil> 
    <Pupil> 
    <Name>Joe</Name> 
    <LastName>Blogs</LastName> 
    <Class>13A</Class> 
    <Age>15</Age> 
    </Pupil> 
</School> 

使用XSLTl我想創建一個PSV,它看起來像以下結構: (SchoolID |位置|名稱|類|年齡)

SCH1122|London|Tom|12B|16 
SCH1122|London|Steve|09A|17 
SCH1122|London|Joe|13A|15 

這是可能的使用XSLT,我會怎麼做呢?

到目前爲止的代碼:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="NamespaceHere" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
xmlns:cs="urn:cs" 
exclude-result-prefixes="cs x msxsl" > 

<xsl:output method="text" omit-xml-declaration="yes" indent="no"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="x:SchoolID"> 
    <xsl:value-of select="text()"/> 
    <xsl:text>|</xsl:text> 
</xsl:template> 

<xsl:template match="x:PostCode"> 
    <xsl:value-of select="text()"/> 
    <xsl:text>|</xsl:text> 
</xsl:template> 

<xsl:template match="x:Name"> 
    <xsl:value-of select="text()"/> 
    <xsl:text>|</xsl:text> 
</xsl:template> 

<xsl:template match="x:Class"> 
    <xsl:value-of select="text()"/> 
    <xsl:text>|</xsl:text> 
</xsl:template> 

<xsl:template match="x:Age"> 
    <xsl:value-of select="text()"/> 
    <xsl:text>|</xsl:text> 
</xsl:template> 

<xsl:template match="text()"> 
    <xsl:apply-templates select="x:Message"/> 
</xsl:template> 

<xsl:template match="x:School"> 
    <xsl:apply-templates select="x:SchoolInfo/x:SchoolID"/> 
    <xsl:apply-templates select="x:SchoolInfo/x:Geographics/x:PostCode"/> 
    <xsl:apply-templates select="x:Pupil/x:Name"/> 
    <xsl:apply-templates select="x:Pupil/x:Class"/> 
    <xsl:apply-templates select="x:Pupil/x:Age"/> 
    <xsl:text>&#0010;</xsl:text> 
</xsl:template> 
</xsl:stylesheet> 

這個工作的時候,我只有一個學生然而,當我得到多個學生輸出變成像下面這樣:

SCH1122 | |倫敦|湯|史蒂夫| 12B | 09A | 16 | 17

+0

到目前爲止,我已經得到它以正確的順序輸出PSV,但是當處理每個瞳孔時,我完全難以理解,所以關於如何在每個瞳孔上循環的指導,同時仍然可以訪問節點在學生之外,將不勝感激 – Mike 2012-03-09 09:19:07

+0

您可以發佈您的代碼到目前爲止? – 2012-03-09 09:21:07

+0

現在添加的代碼:) - 我正在修改代碼,因爲我正在使用的實際文檔的靈敏度,但該代碼應該工作:) – Mike 2012-03-09 09:32:41

回答

1

如何改變你的match="x:School"到:

<xsl:template match="x:School"> 
    <xsl:variable name="parent" select="."/> 
    <xsl:for-each select="x:Pupil"> 
    <xsl:apply-templates select="$parent/x:SchoolInfo/x:SchoolID"/> 
    <xsl:apply-templates select="$parent/x:SchoolInfo/x:Geographics/x:PostCode"/> 
    <xsl:apply-templates select="x:Name"/> 
    <xsl:apply-templates select="x:Class"/> 
    <xsl:apply-templates select="x:Age"/> 
    <xsl:text>&#0010;</xsl:text> 
    </xsl:for-each> 
</xsl:template> 
+0

工作完美,沒想到它變得如此簡單!非常感謝。 – Mike 2012-03-09 10:40:00