2015-07-03 49 views
0

分層XML我有這樣一個XML文件:Sequental XML使用XSL

<?xml version="1.0" encoding="utf-8"?> 
<Items> 
    <Item> 
    <ID>1</ID> 
    <Name>Fun</Name> 
    </Item> 
    <Item> 
    <ID>2</ID> 
    <Name>Sport</Name> 
    <ParentID>1</ParentID> 
    </Item> 
    <Item> 
    <ID>3</ID> 
    <Name>Alcohol</Name> 
    <ParentID>1</ParentID> 
    </Item> 
    <Item> 
    <ID>4</ID> 
    <Name>Cigarettes</Name> 
    <ParentID>1</ParentID> 
    </Item> 
    <Item> 
    <ID>5</ID> 
    <Name>Football</Name> 
    <ParentID>2</ParentID> 
    </Item> 
    <Item> 
    <ID>6</ID> 
    <Name>Whisky</Name> 
    <ParentID>3</ParentID> 
    </Item> 
    <Item> 
    <ID>7</ID> 
    <Name>Camel</Name> 
    <ParentID>4</ParentID> 
    </Item> 
</Items> 

一種XSLT的什麼會產生分層XML?層數可以是各種

<?xml version="1.0" encoding="utf-8"?> 
<Items> 
    <Item ID="1" Name="Fun"> 
    <Item ID="2" Name="Sport" ParentID="1"> 
     <Item ID="5" Name="Football" ParentID="2"/> 
    </Item> 
    <Item ID="3" Name="Alcohol" ParentID="1"> 
     <Item ID="6" Name="Whisky" ParentID="3"/> 
    </Item> 
    <Item ID="4" Name="Cigarettes" ParentID="1"> 
     <Item ID="7" Name="Camel" ParentID="4"/> 
    </Item> 
    </Item> 
</Items> 

回答

1

這是使用key可真有用:

XSLT 1.0

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

<xsl:key name="item-by-parent" match="Item" use="ParentID" /> 

<xsl:template match="/Items"> 
    <xsl:copy> 
     <!-- select progenitors --> 
     <xsl:apply-templates select="Item[not(ParentID)]"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Item"> 
    <Item ID="{ID}" Name="{Name}"> 
     <!-- select children --> 
     <xsl:apply-templates select="key('item-by-parent', ID)"/> 
    </Item> 
</xsl:template> 

</xsl:stylesheet> 
+0

非常感謝您!完美的作品!去探索你的代碼... – Robinson