2016-07-26 57 views
0

Meine Eingabe ist eine Firma org. Struktur in XML, mit mehreren Abteilungen und dort Unterabteilungen. Die Ebene der Unterabteilung in der Org. Die Struktur wird in Tag LEVEL angegeben.XSLT: XML-Liste mit Level zu XML mit rekursiven Knoten

Die Ausgabe sollte auch XML mit mehreren Abteilungen sein. Aber jetzt sollten die Unterabteilungen in einem rekursiven Knoten sein. Sowohl DepartmentList als auch SubDepartmentList bestehen aus einer oder mehreren Abteilungen (oder null) und Abteilung hat SubDepartmentList unter Details.

Das Tool, das ich benutze, ist nur XSLT 1.0, also hoffe ich, dass es möglich ist, in dieser Version zu lösen.

Beispiel bei der Eingabe (nur 1 Abteilung):

<?xml version="1.0" encoding="UTF-8"?> 
<Z_HR_HRM_SYNC_DEPARTMENTS> 
    <IT_ORG> 
     <item> 
      <ORGEH>50013998</ORGEH> 
      <ORGEH_PARENT>50000025</ORGEH_PARENT> 
      <TITLE>Title 50013998</TITLE> 
      <SUB_DEP> 
       <item> 
        <LEVEL>1</LEVEL> 
        <ORGEH>50014000</ORGEH> 
        <ORGEH_PARENT>50013998</ORGEH_PARENT> 
        <TITLE>Title 50014000</TITLE> 
       </item> 
       <item> 
        <LEVEL>1</LEVEL> 
        <ORGEH>50000849</ORGEH> 
        <ORGEH_PARENT>50013998</ORGEH_PARENT> 
        <TITLE>Title 50000849</TITLE> 
       </item> 
       <item> 
        <LEVEL>2</LEVEL> 
        <ORGEH>50017825</ORGEH> 
        <ORGEH_PARENT>50000849</ORGEH_PARENT> 
        <TITLE>Title 50017825</TITLE> 
       </item> 
       <item> 
        <LEVEL>2</LEVEL> 
        <ORGEH>50001684</ORGEH> 
        <ORGEH_PARENT>50000849</ORGEH_PARENT> 
        <TITLE>Title 50001684</TITLE> 
       </item> 
       <item> 
        <LEVEL>2</LEVEL> 
        <ORGEH>50012716</ORGEH> 
        <ORGEH_PARENT>50000849</ORGEH_PARENT> 
        <TITLE>Title 50012716</TITLE> 
       </item> 
       <item> 
        <LEVEL>3</LEVEL> 
        <ORGEH>50001709</ORGEH> 
        <ORGEH_PARENT>50012716</ORGEH_PARENT> 
        <TITLE>Title 50001709</TITLE> 
       </item> 
       <item> 
        <LEVEL>2</LEVEL> 
        <ORGEH>50012713</ORGEH> 
        <ORGEH_PARENT>50000849</ORGEH_PARENT> 
        <TITLE>Title 50012713</TITLE> 
       </item> 
       <item> 
        <LEVEL>2</LEVEL> 
        <ORGEH>50001694</ORGEH> 
        <ORGEH_PARENT>50000849</ORGEH_PARENT> 
        <TITLE>Title 50001694</TITLE> 
       </item> 
       <item> 
        <LEVEL>3</LEVEL> 
        <ORGEH>50010284</ORGEH> 
        <ORGEH_PARENT>50001694</ORGEH_PARENT> 
        <TITLE>Title 50010284</TITLE> 
       </item> 
       <item> 
        <LEVEL>3</LEVEL> 
        <ORGEH>50001695</ORGEH> 
        <ORGEH_PARENT>50001694</ORGEH_PARENT> 
        <TITLE>Title 50001695</TITLE> 
       </item> 
       <item> 
        <LEVEL>4</LEVEL> 
        <ORGEH>50001697</ORGEH> 
        <ORGEH_PARENT>50001695</ORGEH_PARENT> 
        <TITLE>Title 50001697</TITLE> 
       </item> 
      </SUB_DEP> 
     </item> 
    </IT_ORG> 
</Z_HR_HRM_SYNC_DEPARTMENTS> 

Die von Hand gemacht entsprechende Ausgabe:

<?xml version="1.0" encoding="UTF-8"?> 
<ns1:BatchSyncDepartments xmlns:ns1="http://schemas.hr-manager.net/remoting/1.0/"> 
<ns1:request> 
    <ns1:DepartmentList> 
     <ns1:Department> 
      <ns1:Id> 
       <ns1:ThirdPartyId>50013998</ns1:ThirdPartyId> 
      </ns1:Id> 
      <ns1:ParentDepartmentId> 
       <ns1:ThirdPartyId>50000025</ns1:ThirdPartyId> 
      </ns1:ParentDepartmentId> 
      <ns1:Title>Title 50013998</ns1:Title> 
      <ns1:Details> 
       <ns1:SubDepartmentList> 
        <ns1:Department> 
         <ns1:Id> 
          <ns1:ThirdPartyId>50014000</ns1:ThirdPartyId> 
         </ns1:Id> 
         <ns1:ParentDepartmentId> 
          <ns1:ThirdPartyId>50013998</ns1:ThirdPartyId> 
         </ns1:ParentDepartmentId> 
         <ns1:Title>Title 50014000</ns1:Title> 
         <ns1:Details> 
          <ns1:SubDepartmentList/> 
         </ns1:Details> 
        </ns1:Department> 
        <ns1:Department> 
         <ns1:Id> 
          <ns1:ThirdPartyId>50000849</ns1:ThirdPartyId> 
         </ns1:Id> 
         <ns1:ParentDepartmentId> 
          <ns1:ThirdPartyId>50013998</ns1:ThirdPartyId> 
         </ns1:ParentDepartmentId> 
         <ns1:Title>Title 50000849</ns1:Title> 
         <ns1:Details> 
          <ns1:SubDepartmentList> 
           <ns1:Department> 
            <ns1:Id> 
             <ns1:ThirdPartyId>50017825</ns1:ThirdPartyId> 
            </ns1:Id> 
            <ns1:ParentDepartmentId> 
             <ns1:ThirdPartyId>50000849</ns1:ThirdPartyId> 
            </ns1:ParentDepartmentId> 
            <ns1:Title>Title 50017825</ns1:Title> 
            <ns1:Details> 
             <ns1:SubDepartmentList/> 
            </ns1:Details> 
           </ns1:Department> 
           <ns1:Department> 
            <ns1:Id> 
             <ns1:ThirdPartyId>50001684</ns1:ThirdPartyId> 
            </ns1:Id> 
            <ns1:ParentDepartmentId> 
             <ns1:ThirdPartyId>50000849</ns1:ThirdPartyId> 
            </ns1:ParentDepartmentId> 
            <ns1:Title>Title 50001684</ns1:Title> 
            <ns1:Details> 
             <ns1:SubDepartmentList/> 
            </ns1:Details> 
           </ns1:Department> 
           <ns1:Department> 
            <ns1:Id> 
             <ns1:ThirdPartyId>50012716</ns1:ThirdPartyId> 
            </ns1:Id> 
            <ns1:ParentDepartmentId> 
             <ns1:ThirdPartyId>50000849</ns1:ThirdPartyId> 
            </ns1:ParentDepartmentId> 
            <ns1:Title>Title 50012716</ns1:Title> 
            <ns1:Details> 
             <ns1:SubDepartmentList> 
              <ns1:Department> 
               <ns1:Id> 
                <ns1:ThirdPartyId>50001709</ns1:ThirdPartyId> 
               </ns1:Id> 
               <ns1:ParentDepartmentId> 
                <ns1:ThirdPartyId>50012716</ns1:ThirdPartyId> 
               </ns1:ParentDepartmentId> 
               <ns1:Title>Title 50001709</ns1:Title> 
               <ns1:Details> 
                <ns1:SubDepartmentList/> 
               </ns1:Details> 
              </ns1:Department> 
             </ns1:SubDepartmentList> 
            </ns1:Details> 
           </ns1:Department> 
           <ns1:Department> 
            <ns1:Id> 
             <ns1:ThirdPartyId>50012713</ns1:ThirdPartyId> 
            </ns1:Id> 
            <ns1:ParentDepartmentId> 
             <ns1:ThirdPartyId>50000849</ns1:ThirdPartyId> 
            </ns1:ParentDepartmentId> 
            <ns1:Title>Title 50012713</ns1:Title> 
            <ns1:Details> 
             <ns1:SubDepartmentList/> 
            </ns1:Details> 
           </ns1:Department> 
           <ns1:Department> 
            <ns1:Id> 
             <ns1:ThirdPartyId>50001694</ns1:ThirdPartyId> 
            </ns1:Id> 
            <ns1:ParentDepartmentId> 
             <ns1:ThirdPartyId>50000849</ns1:ThirdPartyId> 
            </ns1:ParentDepartmentId> 
            <ns1:Title>Title 50001694</ns1:Title> 
            <ns1:Details> 
             <ns1:SubDepartmentList> 
              <ns1:Department> 
               <ns1:Id> 
                <ns1:ThirdPartyId>50010284</ns1:ThirdPartyId> 
               </ns1:Id> 
               <ns1:ParentDepartmentId> 
                <ns1:ThirdPartyId>50001694</ns1:ThirdPartyId> 
               </ns1:ParentDepartmentId> 
               <ns1:Title>Title 50010284</ns1:Title> 
               <ns1:Details> 
                <ns1:SubDepartmentList/> 
               </ns1:Details> 
              </ns1:Department> 
              <ns1:Department> 
               <ns1:Id> 
                <ns1:ThirdPartyId>50001695</ns1:ThirdPartyId> 
               </ns1:Id> 
               <ns1:ParentDepartmentId> 
                <ns1:ThirdPartyId>50001694</ns1:ThirdPartyId> 
               </ns1:ParentDepartmentId> 
               <ns1:Title>Title 50001695</ns1:Title> 
               <ns1:Details> 
                <ns1:SubDepartmentList> 
                 <ns1:Department> 
                  <ns1:Id> 
                   <ns1:ThirdPartyId>50001697</ns1:ThirdPartyId> 
                  </ns1:Id> 
                  <ns1:ParentDepartmentId> 
                   <ns1:ThirdPartyId>50001695</ns1:ThirdPartyId> 
                  </ns1:ParentDepartmentId> 
                  <ns1:Title>Title 50001697</ns1:Title> 
                  <ns1:Details> 
                   <ns1:SubDepartmentList/> 
                  </ns1:Details> 
                 </ns1:Department> 
                </ns1:SubDepartmentList> 
               </ns1:Details> 
              </ns1:Department> 
             </ns1:SubDepartmentList> 
            </ns1:Details> 
           </ns1:Department> 
          </ns1:SubDepartmentList> 
         </ns1:Details> 
        </ns1:Department> 
       </ns1:SubDepartmentList> 
      </ns1:Details> 
     </ns1:Department> 
    </ns1:DepartmentList> 
</ns1:request> 
</ns1:BatchSyncDepartments> 

ich neu beenden bin zu XSLT und gelöst habe nur die einfachen Sachen, aber jetzt ich bin fest:

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:template match="Z_HR_HRM_SYNC_DEPARTMENTS"> 
    <ns1:BatchSyncDepartments xmlns:ns1="http://schemas.hr-manager.net/remoting/1.0/"> 
     <ns1:request> 
      <ns1:DepartmentList> 
       <xsl:for-each select="IT_ORG/item"> 
        <ns1:Department> 
         <ns1:Id> 
          <ns1:ThirdPartyId> 
           <xsl:value-of select="ORGEH"/> 
          </ns1:ThirdPartyId> 
         </ns1:Id> 
         <ns1:ParentDepartmentId> 
          <ns1:ThirdPartyId> 
           <xsl:value-of select="ORGEH_PARENT"/> 
          </ns1:ThirdPartyId> 
         </ns1:ParentDepartmentId> 
         <ns1:Title> 
          <xsl:value-of select="TITLE"/> 
         </ns1:Title> 
         <ns1:Details> 
          <ns1:SubDepartmentList> 
          </ns1:SubDepartmentList> 
         </ns1:Details> 
        </ns1:Department> 
       </xsl:for-each> 
      </ns1:DepartmentList> 
     </ns1:request> 
    </ns1:BatchSyncDepartments> 
</xsl:template> 
</xsl:stylesheet> 

Ich hoffe, jemand kann mit einigen Eingaben helfen, wie Sie vorgehen.

Antwort

0

Aber jetzt sollten die Unterabteilungen in einem rekursiven Knoten sein.

Dazu sollten Sie eine rekursive Verarbeitung Ansatz verfolgen:

XSLT 1,0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns1="http://schemas.hr-manager.net/remoting/1.0/"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="child" match="item" use="ORGEH_PARENT" /> 

<xsl:template match="Z_HR_HRM_SYNC_DEPARTMENTS"> 
    <ns1:BatchSyncDepartments xmlns:ns1="http://schemas.hr-manager.net/remoting/1.0/"> 
     <ns1:request> 
      <ns1:DepartmentList> 
       <xsl:apply-templates select="IT_ORG/item"/> 
      </ns1:DepartmentList> 
     </ns1:request> 
    </ns1:BatchSyncDepartments> 
</xsl:template>    

<xsl:template match="item"> 
    <ns1:Department> 
     <ns1:Id> 
      <ns1:ThirdPartyId> 
       <xsl:value-of select="ORGEH"/> 
      </ns1:ThirdPartyId> 
     </ns1:Id> 
     <ns1:ParentDepartmentId> 
      <ns1:ThirdPartyId> 
       <xsl:value-of select="ORGEH_PARENT"/> 
      </ns1:ThirdPartyId> 
     </ns1:ParentDepartmentId> 
     <ns1:Title> 
      <xsl:value-of select="TITLE"/> 
     </ns1:Title> 
     <ns1:Details> 
      <ns1:SubDepartmentList> 
       <xsl:apply-templates select="key('child', ORGEH)"/> 
      </ns1:SubDepartmentList> 
     </ns1:Details> 
    </ns1:Department> 
</xsl:template> 

</xsl:stylesheet> 
+0

Vielen Dank. Es funktioniert perfekt. Ich hoffte, dass es auf eine kurze und nette Weise gemacht werden könnte - aber mit meiner begrenzten Erfahrung war es nicht möglich. Es ist immer schön, wenn jemand mit Erfahrung hilft. – Gitte