使用xsl更改元素层次结构,方法是按属性值对元素进行分组,而不考虑属性值?

时间:2013-02-08 18:37:27

标签: xslt xpath node-set

问题

使用xsl,如何通过按属性值对元素进行分组来更改元素层次结构,而不考虑属性值?

问题描述

该文档的上下文如下:xml跟踪软件框架的更改说明(<releaseHistory/>),因为新版本已发布(<build/>)。该框架有几个app / components(<changes app='LibraryA|Driver|...'/>)。更改注释记录新功能或错误修复(<list kind='New|Enhancement'/>)。

我想转换此文档,以便将不同版本中的所有更改注释合并到按“app”属性值和“kind”属性值分组的列表中,并将列表项(<li/>)排序通过'priority'属性。

此外,不应该对'app'和'kind'属性值做出假设。 请注意,如果需要,我可以更改xml的架构,如果架构不理想的话。

当前状态

  • 我能做什么:
    • 检索唯一“app”和“kind”属性值的列表。
    • 一个模板,它将'app'和'kind'作为参数并遍历xml文档以合并其属性与参数匹配的所有元素
  • 缺少什么:
    • '循环'在上面的唯一属性值列表上并应用模板

输入和预期输出

xml文档:

<?xml version="1.0" encoding="UTF-8"?>

<releaseHistory>

 <build>
   <description>A killer update</description>
   <changes app='LibraryA'>
     <list kind='New'>
       <li priority='4'>Added feature about X</li>
       <li priority='2'>Faster code for big matrices</li>
     </list>
     <list kind='Enhancement'>
       <li priority='1'>Fixed integer addition</li>
     </list>
   </changes>
   <changes app='Driver'>
     <list kind='New'>
       <li priority='3'>Supporting new CPU models</li>
       <li priority='4'>Cross-platform-ness</li>
     </list>
   </changes>
 </build>

 <build>
   <description>An update for Easter</description>
   <changes app='LibraryA'>
     <list kind='New'>
       <li priority='1'>New feature about Y</li>
     </list>
     <list kind='Enhancement'>
       <li priority='2'>Fixed bug 63451</li>
     </list>
   </changes>
   <changes app='LibraryVector'>
     <list kind='Enhancement'>
       <li priority='5'>Fixed bug 59382</li>
     </list>
   </changes>
   <changes app='Driver'>
     <list kind='New'>
       <li priority='0'>Compatibility with hardware Z</li>
     </list>
   </changes>
 </build>

</releaseHistory>

预期文件:

<?xml version="1.0" encoding="UTF-8"?>

<mergedHistory>

  <changes app='LibraryA'>
   <list kind='New'>
     <li priority='1'>New feature about Y</li>
     <li priority='2'>Faster code for big matrices</li>
     <li priority='4'>Added feature about X</li>
   </list>
   <list kind='Enhancement'>
      <li priority='1'>Fixed integer addition</li>
      <li priority='2'>Fixed bug 63451</li>
   </list>
  </changes>

  <changes app='Driver'>
    <list kind='New'>
      <li priority='0'>Compatibility with hardware Z</li>
      <li priority='3'>Supporting new CPU models</li>
      <li priority='4'>Cross-platform-ness</li>
    </list>
  </changes>

  <changes app='LibraryVector'>
    <list kind='Enhancement'>
      <li priority='5'>Fixed bug 59382</li>
    </list>
  </changes>

</mergedHistory>

解决方案的一部分

我已经'已经'能够使用xsl列出唯一的'app'和'kind'属性。让我们详细说明xsl

的当前状态
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"
>

检索所有不同的'app'属性值(LibraryA,Driver,...)<changes app='...'/>并将它们存储在变量中(可能是一个参数):

<xsl:key name="appDistinct" match="changes" use="@app"/>
<xsl:variable name="applicationListVarTmp">
  <list>
    <xsl:for-each select="//changes[generate-id() = generate-id(key('appDistinct', @app)[1])]">
      <li>
        <xsl:value-of select="normalize-space(@app)"/>
      </li>
    </xsl:for-each>
  </list>
</xsl:variable>

检索所有不同的'种类'属性值(新增,增强)<list kind='...'/>

<xsl:key name="kindDistinct" match="changes/list" use="@kind"/>
<xsl:variable name="kindListVar">
  <list>
    <xsl:for-each select="//changes/list[generate-id() = generate-id(key('kindDistinct', @kind)[1])]">
      <li>
        <xsl:value-of select="normalize-space(@kind)"/>
      </li>
    </xsl:for-each>
  </list>
</xsl:variable>

使用参数合并给定'app'和'kind'(按优先级排序)的所有<li/>的模板:

<xsl:template name="mergeSameKindChangesForAnApp">
  <xsl:param name="application" />
  <xsl:param name="kindness" />
  <list><xsl:attribute name='kind'><xsl:value-of select="$kindness"/></xsl:attribute>
    <xsl:for-each select="//changes[@app=$application]/list[@kind=$kindness]/li">
      <xsl:sort select="@priority" data-type="number" order="ascending"/>
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:copy-of select="./*"/>
      </xsl:copy>
    </xsl:for-each>
  </list>
</xsl:template>

现在,我被困的地方是关于appListVarkindListVar上的'循环'以应用模板。

如果所有'app'和'kind'都是硬编码的,我可以拨打几个电话:

<xsl:call-template name="mergeSameKindChangesForAnApp">
  <changes app='LibraryA'>
    <xsl:with-param name="application">
      LibraryA
    </xsl:with-param>
    <xsl:with-param name="kindness">
      New
    </xsl:with-param>
  </changes>
</xsl:call-template>

但我想循环使用xml文档中的'app'和'kind'。以exsl:node-set()为例,我可以

<xsl:param name="applicationListVar" select="exsl:node-set($applicationListVarTmp)" />


<xsl:call-template name="mergeSameKindChangesForAnApp">
  <changes app='LibraryA'>
    <xsl:with-param name="application">
      <xsl:value-of select="$applicationListVar/list/li[2]"/>
    </xsl:with-param>
    <xsl:with-param name="kindness">
      New
    </xsl:with-param>
  </changes>
</xsl:call-template>

但是,如何循环$applicationListVar/list/li元素? '循环'听起来不是xslt-ilish,可能(肯定?)这不是正确的方法。

问题很长,我试图将其与实际案例进行简化。

1 个答案:

答案 0 :(得分:1)

这应该这样做:

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

  <xsl:key name="kChange" match="changes" use="@app" />

  <!-- A key for locating <list>s by the combination of their @app and @kind-->
  <xsl:key name="kList" match="changes/list" use="concat(../@app, '+', @kind)" />

  <!-- A node-set of the first instance of each <list> for each distinct
       pair of @app + @kind -->
  <xsl:variable name="distinctLists"
                select="//changes/list[generate-id() = 
                           generate-id(key('kList', 
                                           concat(../@app, '+', @kind) )[1]
                                      )]"/>

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

  <xsl:template match="/*">
    <mergedHistory>
      <!-- Apply templates on distinct <changes> elements -->
      <xsl:apply-templates select="build/changes[generate-id() = 
                                   generate-id(key('kChange', @app)[1])]" />
    </mergedHistory>
  </xsl:template>

  <!-- Each distinct <changes> (based on @app) will be sent to this template -->
  <xsl:template match="changes">
    <changes>
      <xsl:apply-templates select="@*" />

      <!-- Apply templates on each distinct <list> with the same @app
           as the current context-->
      <xsl:apply-templates select="$distinctLists[../@app = current()/@app]" />
    </changes>
  </xsl:template>

  <!-- Each distinct <list> (based on @app and @kind) will be 
       sent to this template -->
  <xsl:template match="list">
    <list>
      <xsl:apply-templates select="@*" />

      <!-- Apply templates on all <li>s below <list>s with the same @app and @kind
           as the current one -->
      <xsl:apply-templates select="key('kList', concat(../@app, '+', @kind))/li">
        <xsl:sort select="@priority" order="ascending" data-type="number"/>
      </xsl:apply-templates>
    </list>
  </xsl:template>
</xsl:stylesheet>

这里要注意的一个技术是根据一对值而不是单个值来获取项目的关键字,并使用它来根据一对值查找不同的实例,然后找到 all < / em>具有相同值对的实例。

当您对样本输入运行时,它会生成请求的输出:

<mergedHistory>
  <changes app="LibraryA">
    <list kind="New">
      <li priority="1">New feature about Y</li>
      <li priority="2">Faster code for big matrices</li>
      <li priority="4">Added feature about X</li>
    </list>
    <list kind="Enhancement">
      <li priority="1">Fixed integer addition</li>
      <li priority="2">Fixed bug 63451</li>
    </list>
  </changes>
  <changes app="Driver">
    <list kind="New">
      <li priority="0">Compatibility with hardware Z</li>
      <li priority="3">Supporting new CPU models</li>
      <li priority="4">Cross-platform-ness</li>
    </list>
  </changes>
  <changes app="LibraryVector">
    <list kind="Enhancement">
      <li priority="5">Fixed bug 59382</li>
    </list>
  </changes>
</mergedHistory>