使用XSL将Sequental XML转换为分层XML

时间:2015-07-03 19:55:28

标签: xml xslt

我有一个像这样的XML文件:

<?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 个答案:

答案 0 :(得分: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>