Announcement

Collapse
No announcement yet.

xsl mehrere XML in eine HTML Tabelle

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • xsl mehrere XML in eine HTML Tabelle

    Hallo liebe Helfer,

    ich habe max. 10 XML-Dateien, die alle im Aufbau identisch sind, sich allerdings in der Anzahl ihrer Einträge unterscheiden. Hier mal exemplarisch drei Dateien:

    Datei1: 0.xml
    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="Start0.xsl"?>
    <Root>
    	<Item lfd-Nr="1" GebSt="0">
    		<AGS>112000</AGS>
    		<Name>erter Text</Name>
    	</Item>
    	<Item lfd-Nr="2" GebSt="0">
    		<AGS>133112</AGS>
    		<Name>zweiter Text</Name>
    	</Item>
    	<Item lfd-Nr="3" GebSt="0">
    		<AGS>433131</AGS>
    		<Name>dritter Text</Name>
    	</Item>
    	<Item lfd-Nr="4" GebSt="0">
    		<AGS>133120</AGS>
    		<Name>vierter Text</Name>
    	</Item>
    </Root>
    Datei2: 1.xml
    Code:
    <?xml version="1.0"?>
    <Root>
    	<Item lfd-Nr="1" GebSt="0">
    		<AGS>112000</AGS>
    		<Name>erster Text</Name>
    	</Item>
    	<Item lfd-Nr="2" GebSt="0">
    		<AGS>133112</AGS>
    		<Name>zweiter Text</Name>
    		</Item>
    	<Item lfd-Nr="3" GebSt="0">
    		<AGS>433131</AGS>
    		<Name>dritter Text</Name>
    	</Item>
    	<Item lfd-Nr="4" GebSt="0">
    		<AGS>133120</AGS>
    		<Name>vierter Text</Name>
    	</Item>
    </Root>
    Datei3: 2.xml
    Code:
    <?xml version="1.0"?>
    <Root>
    	<Item lfd-Nr="1" GebSt="0">
    		<AGS>111000</AGS>
    		<Name>erster geänderter Text</Name>
    	</Item>
    	<Item lfd-Nr="2" GebSt="2">
    		<AGS>133112</AGS>
    		<Name>zweiter Text</Name>
    	</Item>
    </Root>
    Beim Öffnen der ersten Datei (0.xml) mittels Browser hätte ich gerne folgendes Ergebnis:

    HTML Code:
    <html>
      <body>
       <table  border="1" bordercolor="green">
    	<tr>
    	 <td>112000</td>
    	 <td>112000</td>
    	 <td>111000</td>
    	</tr>
    	<tr>
    	 <td>133112</td>
    	 <td>133112</td>
    	 <td>133112</td>
    	</tr>
    	<tr>
    	 <td>433131</td>
    	 <td>433131</td>
             <td></td>
    	</tr>
    	<tr>
    	 <td>133120</td>
    	 <td>133120</td>
             <td></td>
    	</tr>
       </table>
      </body>
    </html>
    Pro Datei also eine Spalte und die Zuordnung in den Zeilen entsprechend der Angaben im Attribut lfd-Nr.

    Wie kann das per xsl umgesetzt werden? Hier mein Denkansatz:

    Datei: Start0.xsl

    Code:
    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
      <xsl:output method="html" indent="yes"/>
    
      <xsl:variable name="File1" select="document('1.xml')" />
      <xsl:variable name="File2" select="document('2.xml')" />
    
      <xsl:template match="/">
        <html>
          <body>
           <xsl:apply-imports /> 
           </body>
       </html>
      </xsl:template>
    
      <xsl:template match="Root">
       <table border="1" bordercolor="green">
         <xsl:for-each select="Item">
    
        	<tr>
    	   <td> <xsl:value-of select="AGS" /></td>
    
               <!-- Item @lfd-Nr = @lfd-Nr der anderen Dateien... -->
               <td> <xsl:value-of select="$File1/Root/Item/AGS" /></td>  
               <td> <xsl:value-of select="$File2/Root/Item/AGS" /></td>
    	</tr>
        </xsl:for-each>
      </table>     	     
      </xsl:template>
    
    </xsl:stylesheet>
    Wäre schön, wenn das machbar wäre. Im Voraus vielen Dank
    Gruß MaxW

  • #2
    Code:
    <xsl:value-of select="$File1/Root/Item[@lfd-Nr = current()/@lfd-Nr]/AGS" />
    usw. sollte reichen.

    Comment


    • #3
      Hallo Martin,
      wunderbar, hab' besten Dank!

      MaxW

      Comment


      • #4
        Die Verarbeitung der weiteren Dokumente lässt sich auch programmatisch lösen:
        Code:
        <xsl:stylesheet
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          version="1.0">
          
          <xsl:output method="html" indent="yes"/>
        
          <xsl:variable 
            name="secondary-docs"
            select="document('1.xml') |
                    document('2.xml')" />
        
          <xsl:key name="item-by-nr" match="Item" use="@lfd-Nr"/>
          
          <xsl:template match="/">
            <html>
              <body>
               <xsl:apply-templates /> 
               </body>
           </html>
          </xsl:template>
        
          <xsl:template match="Root">
           <table border="1" bordercolor="green">
             <tbody>
               <xsl:for-each select="Item">
                 <tr>
        	         <td> <xsl:value-of select="AGS" /></td>
        
        	         <xsl:variable name="lfd-Nr" select="@lfd-Nr"/>
                   <xsl:for-each select="$secondary-docs">
                     <td> <xsl:value-of select="key('item-by-nr', $lfd-Nr)/AGS" /></td>  
                   </xsl:for-each>
                 </tr>
                </xsl:for-each>
              </tbody>
            </table>     	     
          </xsl:template>
        
        </xsl:stylesheet>

        Comment


        • #5
          Hallo Martin,
          vielen Dank für die tollen Lösungen. Da kann man echt was lernen!

          Ich trau' mich kaum zu fragen:
          wie kann man zusätzlich erreichen, dass Zellinhalte, die sich von der vorhergehenden Spalte unterscheiden (auch leer) farblich markiert werden?

          Gruß, MaxW
          Zuletzt editiert von MaxW; 25.06.2011, 21:14.

          Comment


          • #6
            Die farbliche Markierung der Zellinhalte bei Änderungen zur vorhergehenden Spalte konnte ich folgendermaßen lösen:

            Code:
            <xsl:stylesheet
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
              version="1.0">
              <xsl:output method="html" indent="yes"/>
            
              <xsl:variable name="File1" select="document('1.xml')" />
              <xsl:variable name="File2" select="document('2.xml')" />
              <xsl:variable name="File3" select="document('3.xml')" />
              <xsl:variable name="File4" select="document('4.xml')" />
            
            
              <xsl:template match="/">
                <html>
                  <body>
                   <xsl:apply-imports /> 
                   </body>
               </html>
              </xsl:template>
            
              <xsl:template match="Root">
               <table border="1" bordercolor="green">
                 <th> <xsl:value-of select= "count(/Root/Item)" /></th>
                 <th> <xsl:value-of select= "count($File1/Root/Item)" /></th>
                 <th> <xsl:value-of select= "count($File2/Root/Item)" /></th>
                 <th> <xsl:value-of select= "count($File3/Root/Item)" /></th>
                 <th> <xsl:value-of select= "count($File4/Root/Item)" /></th>
            
            
                 <xsl:for-each select="Item">
            
            	<xsl:variable name="Col0" select="AGS" />
            	<xsl:variable name="Col1" select="$File1/Root/Item[@lfd-Nr = current()/@lfd-Nr]/AGS" />
            	<xsl:variable name="Col2" select="$File2/Root/Item[@lfd-Nr = current()/@lfd-Nr]/AGS" />
            	<xsl:variable name="Col3" select="$File3/Root/Item[@lfd-Nr = current()/@lfd-Nr]/AGS" />
            	<xsl:variable name="Col4" select="$File4/Root/Item[@lfd-Nr = current()/@lfd-Nr]/AGS" />
            
            
                	<tr>
            	   
             	   <td> <xsl:value-of select="AGS" /></td>
            
                       <xsl:choose>
                            <xsl:when test="contains($Col1, $Col0)" >
                            	<td> <xsl:value-of select="$Col1" /> </td>
            	        </xsl:when>
            		<xsl:when test="not($Col1)">
            			<td bgcolor= "green"> <xsl:value-of select="$Col1" /> </td>
            		</xsl:when>
            	        <xsl:otherwise>
            			<td bgcolor= "green"> <xsl:value-of select="$Col1" /> </td>
            		</xsl:otherwise>
            	   </xsl:choose>
            
                       <xsl:choose>
                            <xsl:when test="contains($Col2, $Col1)" >
                            	<td> <xsl:value-of select="$Col2" /> </td>
            	        </xsl:when>
            		<xsl:when test="not($Col2)">
            			<td bgcolor= "green"> <xsl:value-of select="$Col2" /> </td>
            		</xsl:when>
            	        <xsl:otherwise>
            			<td bgcolor= "green"> <xsl:value-of select="$Col2" /> </td>
            		</xsl:otherwise>
            	   </xsl:choose>
            
            
                       <xsl:choose>
                            <xsl:when test="contains($Col3, $Col2)" >
                            	<td> <xsl:value-of select="$Col3" /> </td>
            	        </xsl:when>
            		<xsl:when test="not($Col3)">
            			<td bgcolor= "green"> <xsl:value-of select="$Col3" /> </td>
            		</xsl:when>
            	        <xsl:otherwise>
            			<td bgcolor= "green"> <xsl:value-of select="$Col3" /> </td>
            		</xsl:otherwise>
            	   </xsl:choose>
            
                       <xsl:choose>
                            <xsl:when test="contains($Col4, $Col3)" >
                            	<td> <xsl:value-of select="$Col4" /> </td>
            	        </xsl:when>
            		<xsl:when test="not($Col4)">
            			<td bgcolor= "green"> <xsl:value-of select="$Col4" /> </td>
            		</xsl:when>
            	        <xsl:otherwise>
            			<td bgcolor= "green"> <xsl:value-of select="$Col4" /> </td>
            		</xsl:otherwise>
            	   </xsl:choose>
             
            	</tr>
                </xsl:for-each>
              </table>
             
              </xsl:template>
            
            </xsl:stylesheet>
            Aufgrund der beteiligten Eingabedateien ergibt sich folgende Ausgabe:

            HTML Code:
            <html>
             <body>
              <table border="1" bordercolor="green">
            	<th>4</th>
            	<th>3</th>
            	<th>2</th>
            	<th>1</th>
            	<th>1</th>
                  <tr>
            	<td>112000</td>
            	<td>112000</td>
            	<td bgcolor="green">111000</td>
            	<td>111000</td>
            	<td>111000</td>
                  </tr>
                  <tr>
            	<td>133112</td>
            	<td>133112</td>
            	<td>133112</td>
            	<td bgcolor="green"></td>
            	<td></td>
                 </tr>
                 <tr>
            	<td>433131</td>
            	<td bgcolor="green"></td>
            	<td></td>
            	<td></td>
            	<td></td>
                 </tr>
                 <tr>
            	<td>133120</td>
            	<td>133120</td>
            	<td bgcolor="green"></td>
            	<td></td>
            	<td></td>
                 </tr>
              </table>
             </body>
            </html>
            Bestimmt geht das auch eleganter und ldie programmatische Lösung von Martin lässt sich entsprechend anpassen? Eventuell können auch unterschiedliche Farbwerte je Spalte/Datei an "zentraler" Stelle vorgeben?

            Comment

            Working...
            X