XSL Muenchian分组

时间:2015-05-22 11:46:21

标签: xml xslt

我正在尝试使用Muenchian分组(第一次为我),但有一些问题。 我想我犯了一些逻辑错误,我不明白如何找到它。如果我删除输入文件转换中的第一个块工作正常但现在不工作:(

 Source
<?xml version="1.0" encoding="utf-8"?>
<ROOT xsi:schemaLocation="http://www.av.com/pu" Name="StaffManagers" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.av.pu/pu">
    <Data>
        <Part>
            <ReportPart1>
                <Details>
                    <Detail BlokName="Accountant">
                        <Number>1.1.9</Number>
                        <Content>Y</Content>
                        <Id>CInfo</Id>
                        <Collection_Id>11.001.001.009.001</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.1</Number>
                        <Content>College</Content>
                        <Suffix>NameEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.001</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.2</Number>
                        <Content>1 981</Content>
                        <Suffix>YEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.001</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.3</Number>
                        <Content>XX4793</Content>
                        <Suffix>DEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.001</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.4</Number>
                        <Content>Networks</Content>
                        <Suffix>SEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.001</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.5</Number>
                        <Content>Eng</Content>
                        <Suffix>QEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.001</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.1</Number>
                        <Content>Uni</Content>
                        <Suffix>NameEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.002</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.2</Number>
                        <Content>1 999</Content>
                        <Suffix>YEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.002</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.3</Number>
                        <Content>DFC12</Content>
                        <Suffix>DEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.002</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.4</Number>
                        <Content>Acciy</Content>
                        <Suffix>SEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.002</Collection_Id>
                    </Detail>
                    <Detail BlokName="Accountant" Id="CollEduEx">
                        <Number>1.1.9.5</Number>
                        <Content>Eco</Content>
                        <Suffix>QEx</Suffix>
                        <Id>CollEduEx</Id>
                        <Collection_Id>11.001.001.009.002</Collection_Id>
                    </Detail>
                </Details>
            </ReportPart1>
        </Part>
    </Data>
</ROOT>
</i>
  

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:mn="http://www.w3.org/2005/xpath-functions/add"
    version="1.0"
    xmlns:av="http://www.av.pu/pu"
    xmlns:scr="urn:scr.this"
  >

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>


  <xsl:template name="create_element">
    <xsl:param name="element_name"/>
    <xsl:element name="av:{$element_name}">
      <xsl:value-of select="av:Content" />
    </xsl:element>
  </xsl:template>


  <xsl:template name="create_block">
    <xsl:param name="block_name"/>
    <xsl:for-each select="av:Data/av:Part/av:ReportPart1/av:Details/av:Detail[@BlokName=$block_name]">
      <xsl:if test ="av:Content != ''">
        <xsl:choose>
          <xsl:when test="av:Suffix"/>
          <xsl:otherwise>
            <xsl:variable name="elementname">
              <xsl:value-of select="av:Id"/>
            </xsl:variable>
            <xsl:call-template name="create_element">
              <xsl:with-param name="element_name" select="$elementname"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </xsl:for-each>
    <xsl:call-template name="create_collections">
      <xsl:with-param name="block_name" select="$block_name"/>
      <xsl:with-param name="collection_name">CollEduEx</xsl:with-param>
      <xsl:with-param name="collection_element_name">EducationExecutive</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:key name="Collection_Id" match="*" use="av:Collection_Id" />

  <xsl:template name="create_collections">
    <xsl:param name="block_name"/>
    <xsl:param name="collection_name"/>
    <xsl:param name="collection_element_name"/>
    <xsl:element name="{$collection_name}">
      <xsl:for-each select="av:Data/av:Part/av:ReportPart1/av:Details/av:Detail[@BlokName=$block_name and av:Id=$collection_name and generate-id(.)=generate-id(key('Collection_Id',av:Collection_Id))]">
        <xsl:element name="{$collection_element_name}">
          <xsl:for-each select="key('Collection_Id',av:Collection_Id)">
            <xsl:variable name="elementname">
              <xsl:value-of select="av:Suffix"/>
            </xsl:variable>
            <xsl:call-template name="create_element">
              <xsl:with-param name="element_name" select="$elementname"/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
  <xsl:template name="show_name">
    <xsl:param name="element" />
    <xsl:value-of select="normalize-space($element)"/>
  </xsl:template>

  <xsl:template match="av:ROOT">
        <xsl:element name="av:Part1">

              <xsl:call-template name="create_block">
                <xsl:with-param name="block_name">CIO</xsl:with-param>
              </xsl:call-template>

              <xsl:call-template name="create_block">
                <xsl:with-param name="block_name">Accountant</xsl:with-param>
              </xsl:call-template>

        </xsl:element>
  </xsl:template>
</xsl:stylesheet>

预期结果

<av:Part1 xmlns:av="http://www.av.pu/pu">
   <CollEduEx/>
   <av:CInfo>Y</av:CInfo>
   <CollEduEx>
      <EducationExecutive>
         <av:NameEx>College</av:NameEx>
         <av:YEx>1 981</av:YEx>
         <av:DEx>XX4793</av:DEx>
         <av:SEx>Networks</av:SEx>
         <av:QEx>Eng</av:QEx>
      </EducationExecutive>
      <EducationExecutive>
         <av:NameEx>Uni</av:NameEx>
         <av:YEx>1 999</av:YEx>
         <av:DEx>DFC12</av:DEx>
         <av:SEx>Acciy</av:SEx>
         <av:QEx>Eco</av:QEx>
      </EducationExecutive>
   </CollEduEx>
</av:Part1>

1 个答案:

答案 0 :(得分:0)

我已经有一段时间了解Muenchian方法(Jeni Tennison might be useful here for reference)。

我对Muenchian方法的理解很简单就是&#34;&lt; xsl:key&gt;&#34;创建一种查找表。具有相同值的每个元素将具有相同的键值。因此,您可以通过将元素与其类型的第一个键进行比较,将元素分组到文档的不同部分。 (这是对的吗?这有意义吗?)

我不确定我是否可以完全解释或理解它。但是,你有:

<xsl:key name="Collection_Id" match="*" use="av:Collection_Id" />

将此更改为:

<xsl:key name="Collection_Id" match="av:Detail[@Id]" use="av:Collection_Id" />

似乎解决了这个问题。