xsl muenchian分组3级我失去了第二级元素

时间:2015-06-11 19:46:42

标签: xml xslt muenchian-grouping

我一直试图了解muenchian分组,但有一些问题。

 <xsl:key name="CollectionPerson" match="av:Detail[av:Content != '' or av:Code != '']" use="concat(@BlockName,  '|', av:CollectionPerson)" />
 <xsl:key name="SubCollection" match="av:Detail[av:Content != '' or av:Code != '']" use="concat(@BlockName,  '|', av:CollectionPerson, '|', av:Collection_Id)" />

<xsl:key name="lv2" match="av:Detail[av:lv2]" use="concat(@BlockName, '|', av:lv2)" />
<xsl:key name="lv3" match="av:Detail[av:lv3]" use="concat(@BlockName, '|', av:lv3)" />
<xsl:key name="lv4" match="av:Detail[av:lv4]" use="concat(@BlockName, '|', av:lv4)" />
<xsl:key name="lv5" match="av:Detail[av:lv5]" use="concat(@BlockName, '|', av:lv5)" />
<xsl:key name="lv6" match="av:Detail[av:lv6]" use="concat(@BlockName, '|', av:lv6)" />
<xsl:key name="lv7" match="av:Detail[av:lv7]" use="concat(@BlockName, '|', av:lv7)" />
<xsl:key name="Collection_Id" match="av:Detail[@BlockName]" use="av:Collection_Id" />

<xsl:template name="create_block">
  <xsl:param name="block_name"/>
  <xsl:param name="element_link"/>
  <xsl:param name="filter_link"/>
  <xsl:param name="filter"/>
  <xsl:param name="collected_block"/>

  <xsl:variable name="elementf">
    <xsl:value-of select="concat('lv', $filter_link)" />
  </xsl:variable>

  <xsl:variable name="element0">
    <xsl:value-of select="concat('lv', $element_link)" />
  </xsl:variable>

  <xsl:variable name="element1">
    <xsl:value-of select="concat('lv', $element_link + 1)" />
  </xsl:variable>

  <xsl:variable name="element2">
    <xsl:value-of select="concat('lv', $element_link + 2)" />
  </xsl:variable>

  <xsl:variable name="element3">
    <xsl:value-of select="concat('lv', $element_link + 3)" />
  </xsl:variable>

  <xsl:variable name="element4">
    <xsl:value-of select="concat('lv', $element_link + 4)" />
  </xsl:variable>

  <xsl:variable name="element5">
    <xsl:value-of select="concat('lv', $element_link + 5)" />
  </xsl:variable>

  <xsl:variable name="element6">
    <xsl:value-of select="concat('lv', $element_link + 6)" />
  </xsl:variable>


  <xsl:for-each select="av:Data/av:Part/av:ReportPart1/av:Details/av:Detail[@BlockName=$block_name and *[name() = $elementf] = $filter and generate-id(.)=generate-id(key($element0, concat(@BlockName, '|', *[name() = $element0])))]">
    <xsl:for-each select="key($element0, concat(@BlockName, '|', *[name() = $element0]))[@BlockName=$block_name and generate-id(.) = generate-id(key('CollectionPerson', concat(@BlockName, '|', av:CollectionPerson))[1])]">
      <xsl:element name="av:{*[name() = $element2]}">
        <xsl:for-each select="key('CollectionPerson', concat(@BlockName, '|', av:CollectionPerson))[generate-id(.) = generate-id(key('SubCollection', concat(@BlockName, '|', av:CollectionPerson, '|', av:Collection_Id)))] ">
          <xsl:choose>
            <xsl:when test="av:Collection_Id=''">
              <xsl:call-template name="create_element">
                <xsl:with-param name="element_name" select="*[name() = $element3]"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:if test="*[name() = $element6]">
                <xsl:element name="av:{*[name() = $element3]}">
                  <xsl:element name="av:{*[name() = $element4]}">
                    <xsl:element name="av:{*[name() = $element5]}">
                      <xsl:for-each select="key('SubCollection',concat(@BlockName, '|', av:CollectionPerson, '|', av:Collection_Id))">
                        <xsl:sort select="av:Number"/>
                        <xsl:call-template name="create_element">
                          <xsl:with-param name="element_name" select="*[name() = $element6]"/>
                        </xsl:call-template>
                      </xsl:for-each>
                    </xsl:element>
                  </xsl:element>
                </xsl:element>
              </xsl:if>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
      </xsl:element>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

它会跳过除第一个之外的所有元素,这些元素没有Collection_Id。 在 - &GT;&GT;

<Detail BlockName="S">
  <Number>1000</Number>
  <Information>FirstName</Information>
  <Content>John</Content>
  <Code></Code>
  <lv1>S</lv1>
  <lv2>members</lv2>
  <lv3>member</lv3>
  <lv4>member_1</lv4>
  <lv5>FirstName</lv5>
  <CollectionPerson>2</CollectionPerson>
  <Collection_Id></Collection_Id>
</Detail>
<Detail BlockName="S">
  <Number>1100</Number>
  <Information>SecondName</Information>
  <Content>Smith</Content>
  <Code></Code>
  <lv1>S</lv1>
  <lv2>members</lv2>
  <lv3>member</lv3>
  <lv4>member_1</lv4>
  <lv5>SecondName</lv5>
  <CollectionPerson>2</CollectionPerson>
  <Collection_Id></Collection_Id>
</Detail> 
<Detail BlockName="S">
  <Number>1200</Number>
  <Information>Value</Information>
  <Content>100</Content>
  <Code></Code>
  <lv1>S</lv1>
  <lv2>members</lv2>
  <lv3>member</lv3>
  <lv4>member_1</lv4>
  <lv5>Acts</lv5>
  <lv6>Act</lv6>
  <lv7>Act_1</lv7>
  <lv8>Value</lv8>
  <CollectionPerson>2</CollectionPerson>
  <Collection_Id>101</Collection_Id>
</Detail>

结果

    <av:members>
      <av:member>
        <av:member_1>
          <av:FirstName>John</av:FirstName>
          <av:SecondName>Smith</av:SecondName>  <!-- This element not appear  -->
          <av:Acts>
            <av:Act>
              <av:Act_1>
                <av:Qty>10</av:Qty>
                <av:Value>100</av:Value>  
              </av:Act_1>
            </av:Act>
          </av:Acts>
          <av:member_1>
        <av:member>
      <av:members>

我很感激我在哪里出错了。谢谢!

1 个答案:

答案 0 :(得分:0)

使用以下过程将Muenchian分组扩展到多个级别:

  • 对于第一级,请创建与第一个键匹配的xsl-template
  • 对于每个后续级别,创建一个与该级别的密钥匹配的for-each
  • 要进行调试,请使用属性值模板打印未知值

<强>参考