如何使用XSLT组装XML层次结构

时间:2012-05-25 23:21:23

标签: xslt

鉴于此XML

<Xml>
    <Thing id="1"  >
        <Foo id="11" parentId="12"/>
        <Foo id="12"/>
    </Thing>
    <Thing id="2" parentId="1" />
    <Thing id="3" parentId="2" />
    <Thing id="4">
        <Foo id="11" parentId="15"/>
        <Foo id="12" parentId="14"/>
        <Foo id="13" parentId="11"/>
        <Foo id="14" parentId="15"/>
        <Foo id="15"/>      
    </Thing>
</Xml>

我想抓住每个兄弟姐妹的集合,并将它们组装到自己的层次结构中。

每个具有parentId值的“Thing”节点都应嵌套在相应的Thing节点下。 每个具有parentId值的“Foo”节点都应嵌套在相应的Foo节点下 - 但仅限于其兄弟节点。该示例有两组Foo兄弟。

我正在努力创造这个:

<Xml>
    <Thing id="1" >
        <Foo id="12">
            <Foo id="11" parentId="12"/>
        </Foo>        
        <Thing id="2" parentId="1" >
            <Thing id="3" parentId="2" />
        </Thing>
    </Thing>            
    <Thing id="4" >
        <Foo id="14" parentId="12">
            <Foo id="12" parentId="14"/>
        </Foo>
        <Foo id="15">
            <Foo id="11" parentId="15">
                <Foo id="13" parentId="11"/>
            </Foo>
        </Foo>
    </Thing>
</Xml>

这个例子很接近: How can I use XSLT 1.0 to add structure to a non-heirarchal XML file?

我使用了身份模板来保留所有节点和属性。然后我想要一个重写模板匹配所有具有兄弟(后面或前面)的节点,使兄弟的@parentId值等于我的@id值。我最接近的是硬编码id / parentId值以匹配。

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

    <xsl:output indent="yes"/>

    <!-- override identity rule with template to match on
        a node who has siblings, where sibling/@parentId == ./@id
    -->
    <xsl:template match="node()[@id='1' and (preceding-sibling::*[@parentId = 1] or following-sibling::*[@parentId = 1])]">
       <captured>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
       </captured>
    </xsl:template>  


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


</xsl:stylesheet>

我无法看到如何根据parentId值获取当前节点@id值以在Xpath匹配兄弟的谓词中使用。

然后我想在它下面嵌套当前节点的兄弟节点,其中兄弟姐妹@ParentId等于我的@id。

1 个答案:

答案 0 :(得分:4)

首先请注意提供的XML文档中的错误

    <Foo id="12" parentId="14"/>
    <Foo id="13" parentId="11"/>
    <Foo id="14" parentId="12"/>

Fooid 12和Foo14之间存在循环关系。这是一个循环,而不是“层次结构”。此外,这两个Foo元素从层次结构顶部无法访问请更正

此转化

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

 <xsl:key name="kElemById" match="*"
   use="concat(generate-id(..), '+', @id)"/>
 <xsl:key match="*" name="kDescendants"
  use="concat(generate-id(key('kElemById', 
                              concat(generate-id(..), '+',@parentId))),
             '+', @parentId)"/>

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

 <xsl:template match="/*">
  <Xml>
   <xsl:apply-templates select="*[not(@parentId)]"/>
  </Xml>
 </xsl:template>

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

    <xsl:apply-templates select="*[not(@parentId)]"/>

    <xsl:apply-templates select=
       "key('kDescendants', concat(generate-id(), '+',  @id))"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<Xml>
    <Thing id="1"  >
        <Foo id="11" parentId="12"/>
        <Foo id="12"/>
    </Thing>
    <Thing id="2" parentId="1" />
    <Thing id="3" parentId="2" />
    <Thing id="4">
        <Foo id="11" parentId="15"/>
        <Foo id="12" parentId="14"/>
        <Foo id="13" parentId="11"/>
        <Foo id="14" parentId="12"/>
        <Foo id="15"/>
    </Thing>
</Xml>

会产生正确的结果,不包括任何无法访问的元素

<Xml>
   <Thing id="1">
      <Foo id="12">
         <Foo id="11" parentId="12"/>
      </Foo>
      <Thing id="2" parentId="1">
         <Thing id="3" parentId="2"/>
      </Thing>
   </Thing>
   <Thing id="4">
      <Foo id="15">
         <Foo id="11" parentId="15">
            <Foo id="13" parentId="11"/>
         </Foo>
      </Foo>
   </Thing>
</Xml>
相关问题