Announcement

Collapse
No announcement yet.

XSLT Geschwister_Elemente zusammen führen

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

  • XSLT Geschwister_Elemente zusammen führen

    Hallo,

    Ich hab eine Problem beim umwandeln einer Xml-Datei und bin langsam am verzeifeln.

    Quell_Xml:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Inventory>
    	<test>
    		<HW>
    			<1>aa</1>
    			<2>bb</2>
    			<3>cc</3>
    			<4>dd</4>
    			<5>ee</5>
    		</HW>
    		<SW>
    			<a>11</a>
    			<a>22</a>
    			<a>33</a>
    			<a>44</a>
    			<a>55</a>
    		</SW>
    		<HW>
    			<1>ab</1>
    			<2>cd</2>
    			<3>ef</3>
    			<4>gh</4>
    			<5>ij</5>
    		</HW>
    		<SW>
    			<a>12</a>
    			<a>23</a>
    			<a>45</a>
    			<a>56</a>
    			<a>67</a>
    		</SW>
    	</test>
    </Inventory>
    Die Ziel_Xml soll so aussehen:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <Inventory>
    	<test>
    		<HW>
    			<1>aa</1>
    			<2>bb</2>
    			<3>cc</3>
    			<4>dd</4>
    			<5>ee</5>
    			<a>11</a>
    			<a>22</a>
    			<a>33</a>
    			<a>44</a>
    			<a>55</a>
    		</HW>
    		<HW>
    			<1>ab</1>
    			<2>cd</2>
    			<3>ef</3>
    			<4>gh</4>
    			<5>ij</5>
    			<a>12</a>
    			<a>23</a>
    			<a>45</a>
    			<a>56</a>
    			<a>67</a>
    		</HW>
    	</test>
    </Inventory>
    Frage:
    Ist das überhaupt möglich?
    Und wenn ja wie?

    ich benutze XSLT 2.0 und den SaxonHE9.4N.

    Bin für jede Antwort dankbar, die mir weiter hilft

    freundliche Grüße
    Marcus

  • #2
    Das Ausgangsdokument ist nicht wohlgeformt, Elementnamen dürfen nicht mit Ziffern beginnen. Ansonsten hilft ein Ansatz in dieser Richtung:
    [highlight=xml]<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@* | node()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="HW">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <xsl:copy-of select="following-sibling::SW[1]/*"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="SW"/>

    </xsl:stylesheet>[/highlight]

    Comment


    • #3
      vielen dank für die schnelle hilfe und den ansatz. Wird mir auf jedenfall weiter helfen.

      Comment


      • #4
        Als Alternative mit XSLT 2.0 ist auch die Benutzung von for-each-group starting-with="HW" möglich:
        Code:
        <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          exclude-result-prefixes="xs">
          
          <xsl:strip-space elements="*"/>
          <xsl:output indent="yes"/>
          
          <xsl:template match="@* | node()">
            <xsl:copy>
              <xsl:apply-templates select="@* , node()"/>
            </xsl:copy>
          </xsl:template>
          
          <xsl:template match="test">
            <xsl:copy>
              <xsl:for-each-group select="*" group-starting-with="HW">
                <xsl:copy>
                  <xsl:apply-templates select="current-group()/node()"/>
                </xsl:copy>
              </xsl:for-each-group>
            </xsl:copy>
          </xsl:template>
          
        </xsl:stylesheet>

        Comment

        Working...
        X