如何创建一个单一的元素集合(其中,元素名称是复杂的),使用XSLT的子集? [英] How to create subsets of a single set of elements (where element names are complex) with XSLT?

查看:142
本文介绍了如何创建一个单一的元素集合(其中,元素名称是复杂的),使用XSLT的子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在延续的问题我问过关于<一个href="http://stackoverflow.com/questions/1068011/how-to-create-subsets-of-a-single-set-of-elements-with-xslt">"How创建一组与XSLT元素的子集?

In continuation of the question I had asked concerning "How to create subsets of a single set of elements with XSLT?"

我希望进一步把我的问题,一步到位: 我原本给下面的XML作为原始的:

I wish to take my problem one step further: I had originally given the following XML as the original:

<Set>
   <Element name="Superset1_Set1_Element1"/>
   <Element name="Superset1_Set1_Element2"/>
   <Element name="Superset1_Set2_Element1"/>
   <Element name="Superset2_Set1_Element1"/>
   <Element name="Superset2_Set2_Element1"/>
</Set>

和曾要求XSL转换生成以下的输出:

And had asked for the XSL Transformation to produce the following output:

<Superset name="Superset1">
   <Set name="Set1">
       <Element name="Element1"/>
       <Element name="Element2"/>
   </Set>
   <Set name="Set2">
       <Element name="Element1"/>
   </Set>
</Superset>
<Superset name="Superset2">
   <Set name="Set1">
       <Element name="Element1"/>
   </Set>
   <Set name="Set2">
       <Element name="Element1"/>
   </Set>
</Superset>

托默勒格和 给了我一个可行的解决方案annakata。我选择了托默勒格的,由于它的使用模板是,在我看来更可读。

Both Tomalak and annakata had given me a working solution. I had chosen Tomalak's due to it's use of templates which is, in my opinion more human readable.

现在的问题是,我的XML实际上是下面的形式:

The problem is that my XML is actually of the form:

<Set>
   <Element name="Classic_Authors_Dante_Alighieri_The_Divine_Comedy"/>
   <Element name="Classic_Authors_Dante_Alighieri_Convivio"/>
   <Element name="Classic_Authors_Miguel_de_Cervantes_Saavedra_Don_Quixote"/>
   <Element name="Contemporary_Authors_Stephen_King_Just_After_Sunset"/>
   <Element name="Contemporary_Authors_Stephen_King_Desperation"/>
</Set>

超集,集合和元素都有不同在其中下划线量。 在上面的例子中有两个超集:'Classic_Authors'和'Contemporary_Authors'。三组是'Dante_Alighieri','Miguel_de_Cervantes_Saav​​edra'和'Stephen_King'

Supersets, sets and elements have varying amounts of underscores within them. In the example above There are two supersets: 'Classic_Authors' and 'Contemporary_Authors'. The three sets are 'Dante_Alighieri', 'Miguel_de_Cervantes_Saavedra' and 'Stephen_King'.

输出XML然后应该是:

The output XML should then be:

<Superset name="Classic_Authors">
   <Set name="Dante_Alighieri">
       <Element name="The_Divine_Comedy"/>
       <Element name="Convivio"/>
   </Set>
   <Set name="Miguel_de_Cervantes_Saavedra">
       <Element name="Don_Quixote"/>
   </Set>
</Superset>
<Superset name="Contemporary_Authors">
   <Set name="Stephen_King">
       <Element name="Just_After_Sunset"/>
       <Element name="Desperation"/>
   </Set>
</Superset>

那么如何,我可以用托默勒格的解决方案?也就是说,我应该怎么prepare我的XML使用他的算法?它可以在一个单一的XSLT来实现?可能会有另一种解决方案?

How then, can I use Tomalak's solution? That is, how should I prepare my xml to use his algorithm? Can it be done in a single XSLT? Might there be another solution?

谢谢大家非常喜欢!

推荐答案

当我在你的previous问题我的回答评论说,你需要的是包含固定的和已知的组名称,然后文件可以开始解决这个问题。理想情况下,它是结构化的,像这样的:

As I said in the comments to my answer in your previous question, you'll need a file that contains the fixed and known set names before you can begin to solve this. Ideally, it is structured, like this:

<!-- SetNames.xml --->
<names>
  <Superset name="Classic_Authors">
    <Set name="Dante_Alighieri" />
    <Set name="Miguel_de_Cervantes_Saavedra" />
  </Superset>
  <Superset name="Contemporary_Authors">
    <Set name="Stephen_King" />
  </Superset>
</names>

如果没有这样的文件中的问题将不会被解的。现在,你有很好的结构化的组名称,你可以根据它的分组(从本质上讲,它已经在输出格式,所有你需要做的是匹配数据反对票):

Without such a file the problem will not be solvable. Now that you have nice structured set of names, you can do the grouping based on it (essentially, it is already in the output format, all you need to do is match your data against it):

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:param name="pSetFile" select="'SetNames.xml'" />
  <xsl:variable name="root" select="/" />

  <xsl:template match="/Set">
    <xsl:copy>
      <xsl:variable name="vSetDoc" select="document($pSetFile)" />
      <xsl:apply-templates select="$vSetDoc/names/Superset">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
    </xsl:copy> 
  </xsl:template>

  <xsl:template match="Superset">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates select="Set">
        <xsl:sort select="@name" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Set">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:variable name="vPrefix" select="
        concat(../@name, '_', @name, '_')
      " />
      <xsl:apply-templates select="
        $root/Set/Element[starts-with(@name, $vPrefix)]
      ">
        <xsl:sort select="@name" />
        <xsl:with-param name="pPrefix" select="$vPrefix" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Element">
    <xsl:param name="pPrefix" select="''" />

    <xsl:copy>
      <xsl:attribute name="name">
        <xsl:value-of select="substring-after(@name, $pPrefix)" />
      </xsl:attribute>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

当应用到你的输入,这将产生:

When applied to your input, this produces:

<Set>
  <Superset name="Classic_Authors">
    <Set name="Dante_Alighieri">
      <Element name="Convivio" />
      <Element name="The_Divine_Comedy" />
    </Set>
    <Set name="Miguel_de_Cervantes_Saavedra">
      <Element name="Don_Quixote" />
    </Set>
  </Superset>
  <Superset name="Contemporary_Authors">
    <Set name="Stephen_King">
      <Element name="Desperation" />
      <Element name="Just_After_Sunset" />
    </Set>
  </Superset>
</Set>

由于 SetNames.xml 基本上已经分组,进一步(的Muenchian)分组不会是必要的。最慢的前pression在上面会是这样的:

Since SetNames.xml basically is already grouped, further (Muenchian) grouping will not be necessary. The slowest expression in the above will be this:

$root/Set/Element[starts-with(@name, $vPrefix)]

这个表扫描式的前pression也正是一个&LT; XSL:关键&GT; 将是有益的,但​​由于问题的性质它不能在此处使用。

This "table scan" type of expression is exactly where an <xsl:key> would be helpful, but due to the nature of the problem it can't be used here.

这篇关于如何创建一个单一的元素集合(其中,元素名称是复杂的),使用XSLT的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆