使用重写参数或数组的不同值xslt

时间:2012-03-02 02:33:58

标签: xslt

我的xml看起来像这样:

<topics>
   <topic name="topic1" />
 <topic name="topic2" />
 <topic name="topic3" />
 <topic name="topic4" />
 <topic name="topic5" />
 <topic name="topic6" /> 
<topic name="topic7" />
</topics>
<supertopics>
  <supertopic title="supertopic1" name="1;topic1;#2;#topic2;3;#topic3" />
  <supertopic title="supertopic2" name="4;#topic4;#7;#topic7" />
    <supertopic title="supertopic3" name="2;#topic2;#3;#topic3" />
  <supertopic title="supertopic4" name="5;#topic5;#7;#topic7" />
  <supertopic title="supertopic5" name="3;#topic3;#7;#topic7" />
  <supertopic title="supertopic6" name="4;#topic4;#7;#topic7" />
  <supertopic title="supertopic7" name="5;#topic5;#7;#topic7" />
  <supertopic title="supertopic8" name="2;#topic2;#6;#topic6" />
  <supertopic title="supertopic9" name="5;#topic6;#7;#topic7" />
  <supertopic title="supertopic10" name="3;#topic3;#4;#topic4" />
</supertopics>

我基本上每个主题需要1个超单位。所以这意味着我有7个主题,我想要7个与它相关的最新超文本。我也通过排序来约会它,但主要的是我希望这7个超级特征是独一无二的,因为每个主题都有多个超文本。

所以我希望我的输出是这样的:

supertopic1 (topic1 is associated to supertopic1)
supertopic3 (topic2 is associated to supertopic1 but as its already there i want it to look for next supertopic its associated to)
supertopic5 (topic3)
supertopic2 (topic4)
supertopic4 (topic5)
supertopic8 (topic6)
supertopic6 (topic7)

我正在使用xsl 1.0

我试图用它来实现它,但我找不到任何方法来做到这一点:

<xsl:param name="FilteredAssets1">
 <stopic></stopic>
  <ttopic></ttopic>
</xsl:param>
    <xsl:for-each select="topics/topic/@name">
            <xsl:variable name="topicname">
                <xsl:value-of select="."></xsl:value-of>
            </xsl:variable>
   <xsl:for-each select="/supertopics/supertopic[contains(@name,$topicname)]">
             <xsl:if test="not(contains(msxsl:node-set($FilteredAssets1)/stopic,@title)) and not(contains(msxsl:node-set($FilteredAssets1)/ttopic,$programname))">
        <stopic><xsl:value-of select="@title"></xsl:value-of></stopic>
        <ttopic><xsl:value-of select="$programname"></xsl:value-of></ttopic>

            </xsl:if>
            </xsl:for-each>         
         </xsl:for-each>  

1 个答案:

答案 0 :(得分:0)

此转化

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

 <xsl:variable name="vSupers"
      select="/*/supertopics/supertopic"/>

 <xsl:template match="/*">
  <xsl:apply-templates select="topics/topic[1]"/>
 </xsl:template>

 <xsl:template match="topic">
   <xsl:param name="pUsedSupers" select="/.."/>

   <xsl:variable name="vNewSuper" select=
   "$vSupers
       [not(@title = $pUsedSupers/@title)
      and
        contains(concat(@name, ';'),
                 concat('#', current()/@name, ';')
                )
       ]
        [1]
   "/>

   <xsl:value-of select="concat('&#xA;', $vNewSuper/@title)"/>

   <xsl:apply-templates select="following-sibling::topic[1]">
    <xsl:with-param name="pUsedSupers"
     select="$pUsedSupers | $vNewSuper"/>
   </xsl:apply-templates>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文本(包含在单个顶部元素中,以形成格式良好的XML文档,并且还可以修正我认为是{{1}的拼写错误}):

supertopic1

生成想要的正确结果

<t>
    <topics>
        <topic name="topic1" />
        <topic name="topic2" />
        <topic name="topic3" />
        <topic name="topic4" />
        <topic name="topic5" />
        <topic name="topic6" />
        <topic name="topic7" />
    </topics>
    <supertopics>
        <supertopic title="supertopic1" name="1;#topic1;#2;#topic2;3;#topic3" />
        <supertopic title="supertopic2" name="4;#topic4;#7;#topic7" />
        <supertopic title="supertopic3" name="2;#topic2;#3;#topic3" />
        <supertopic title="supertopic4" name="5;#topic5;#7;#topic7" />
        <supertopic title="supertopic5" name="3;#topic3;#7;#topic7" />
        <supertopic title="supertopic6" name="4;#topic4;#7;#topic7" />
        <supertopic title="supertopic7" name="5;#topic5;#7;#topic7" />
        <supertopic title="supertopic8" name="2;#topic2;#6;#topic6" />
        <supertopic title="supertopic9" name="5;#topic6;#7;#topic7" />
        <supertopic title="supertopic10" name="3;#topic3;#4;#topic4" />
    </supertopics>
</t>