1
votes

I'm trying to use Muenchian grouping (first time for me) but have some ploblems. I think I made some logical error and I do not understand how to find it. If I delete first block in input file transformation work fine but do not work now :(

 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>

Expected Result

<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
"If I delete first block in input file transformation work fine but do not work now" What is the exact issue? What is "work fine" and "do not work now"??potame

1 Answers

0
votes

It has been a while since I understood the Muenchian Method (Jeni Tennison might be useful here for reference).

Loosely my understanding of the Muenchian Method is that the "<xsl:key>" creates a kind of lookup table. Every element with the same value will have the same key value. So you can group elements in different parts of the document by comparing their key against the first key of their kind. (Is that right? Does that make sense?)

I'm not sure I can fully explain or understand it. However, where you have:

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

changing this to:

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

seems to fix it.