2015-05-22 130 views
1

我想使用Muenchian分組(第一次爲我),但有一些ploblems。 我想我犯了一些邏輯錯誤,但我不明白如何找到它。如果刪除第一個塊中輸入文件轉換工作正常,但現在不工作:(XSL 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

「*如果我刪除輸入文件轉換工作的第一個塊,但現在不工作*」確切的問題是什麼?什麼是「*工作正常*」和「*現在不工作*」? – potame

回答

0

這是一個而自從我明白了Muenchian方法(Jeni Tennison might be useful here for reference)。

鬆散地我對Muenchian方法的理解是,「< xsl:key >」創建了一種查找表。每個具有相同值的元素將具有相同的鍵值。所以你可以通過比較它們的關鍵字和類型的第一個關鍵字來將文檔的不同部分的元素進行分組。 (是嗎?是否有道理?)

我不確定我能否完全解釋或理解它。但是,如果您有:

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

改變這一到:

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

似乎解決它。