1

我在这里看到了这个问题的一些变化,但我不确定如何将它们应用于我的情况,所以我希望也许有人可以在这里帮助我。

我有一个类似于以下格式的平面 XML 文件:

<item id="1"/>
<item id="1.1"/>
<item id="1.1.1"/>
<item id="1.1.2"/>
<item id="1.1.2.1"/>
<item id="1.2"/>
<item id="1.3"/>

我希望根据 id 属性分层设置标签,如下所示:

<item id="1">
  <item id="1.1">
    <item id="1.1.1"/>
    <item id="1.1.2">
      <item id="1.1.2.1"/>
    </item>
  </item>
  <item id="1.2"/>
  <item id="1.3"/>
</item>

一些 id 值有两位数字(例如,“1.2.3.15.1”),这使得比较它们更具挑战性。

帮助?

4

2 回答 2

3

选择正确的节点可能很棘手,但由于您没有层次结构,这适用于您的示例输入(如果您向其中添加根元素)

  <!-- start somewhere -->
  <xsl:template match="/root">
    <root>
      <!-- select all with no . in the id -->
      <xsl:apply-templates  select="//item[string-length(translate(@id,'1234567890',''))=0]" />
    </root>
  </xsl:template>

  <xsl:template match="item">
    <xsl:variable name="id" select="@id"/>
    <!-- how many . have we ? That is how deep we are and add 1 -->
    <xsl:variable name="deep" select="string-length(translate(@id,'1234567890',''))+1" />
    <xsl:copy>
      <!-- copy attribs over -->
      <xsl:apply-templates select="@*"/> 
      <!-- select all nodes that start with our curent id,  
           select nodes that are just one level below us 
           and don't select our selfs-->
      <xsl:apply-templates select="//item[starts-with(@id, $id) and string-length(translate(@id,'1234567890',''))=$deep and not(@id=$id)]"/>
    </xsl:copy>
  </xsl:template>

  <!-- copy attribs -->
  <xsl:template match="@*">
    <xsl:copy />
  </xsl:template>
于 2012-08-24T21:04:38.140 回答
1

I. 这是一个简单的 XSLT 2.0 解决方案(类似的 XSLT 1.0 解决方案紧随其后):

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

 <xsl:template match="/*">
     <xsl:sequence select="my:grouping(*, 1)"/>
 </xsl:template>

 <xsl:function name="my:grouping" as="element()*">
  <xsl:param name="pNodes" as="element()*"/>
  <xsl:param name="pLevel" as="xs:integer"/>

  <xsl:if test="$pNodes">
      <xsl:for-each-group select="$pNodes" group-by="tokenize(@id, '\.')[$pLevel]">
       <xsl:copy>
         <xsl:copy-of select="@*"/>
         <xsl:sequence select="
          my:grouping(current-group()[tokenize(@id, '\.')[$pLevel+1]], $pLevel+1)"/>
       </xsl:copy>
  </xsl:for-each-group>
  </xsl:if>
 </xsl:function>
</xsl:stylesheet>

当将此转换应用于此 XML 文档(提供的 XML 片段,包装在单个顶部元素中以使其成为格式良好的 XML 文档)时:

<t>
    <item id="1"/>
    <item id="1.1"/>
    <item id="1.1.1"/>
    <item id="1.1.2"/>
    <item id="1.1.2.1"/>
    <item id="1.2"/>
    <item id="1.3"/>
</t>

产生了想要的正确结果

<item id="1">
   <item id="1.1">
      <item id="1.1.1"/>
      <item id="1.1.2">
         <item id="1.1.2.1"/>
      </item>
   </item>
   <item id="1.2"/>
   <item id="1.3"/>
</item>

二、这是一个类似的 XSLT 1.0 解决方案

<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="kFollowing" match="item"
   use="generate-id(preceding-sibling::*
                     [string-length(current()/@id) > string-length(@id)
                    and
                      starts-with(current()/@id, concat(@id, '.'))]
                       [1])"/>

 <xsl:template match="/*">
     <xsl:call-template name="grouping">
      <xsl:with-param name="pNodes" select="*"/>
      <xsl:with-param name="pLevel" select="1"/>
     </xsl:call-template>
 </xsl:template>

 <xsl:template name="grouping">
  <xsl:param name="pNodes"/>
  <xsl:param name="pLevel" select="1"/>

  <xsl:for-each select=
    "$pNodes[$pLevel > string-length(@id) - string-length(translate(@id, '.', ''))]">
   <xsl:copy>
     <xsl:copy-of select="@*"/>

     <xsl:call-template name="grouping">
       <xsl:with-param name="pNodes" select="key('kFollowing', generate-id())"/>
         <xsl:with-param name="pLevel" select="$pLevel+1"/>
     </xsl:call-template>
   </xsl:copy>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当这个 XSLT 1.0 转换应用于同一个文档(上图)时,会产生同样想要的正确结果

<item id="1">
   <item id="1.1">
      <item id="1.1.1"/>
      <item id="1.1.2">
         <item id="1.1.2.1"/>
      </item>
   </item>
   <item id="1.2"/>
   <item id="1.3"/>
</item>
于 2012-08-25T03:58:13.850 回答