2016-04-22 1 views
1

im folgende Beispiel die Anforderung <userSearchRecords> Elemente zu fusionieren (durch <userAccount/login> Element identifiziert) durch <otherAttributes> Kind-Element zu bewegen, wenn nötig, das heißt: wenn 2 <userSearchRecords> Elemente der XML, verbinden sie durch Bewegen des <otherAttributes> von dem zweiten <userSearchRecords> zu dem ersten und Entfernen des zweiten <userSearchRecords> Elements; Ansonsten lass es so wie es ist.XSLT merge Knoten mit demselben Identifikator Elemente

Beispiel XML-Eingabe:

<SearchReply_OutputVariable> 
    <part name="parameters"> 
    <searchResponse> 
     <userSearchRecords> 
     <userAccount> 
      <login>A</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>1</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>B</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>2</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>C</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>1</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>D</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>1</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>G</login> 
      <otherAttributes> 
      <name>Y</name> 
      <value>5</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>C</login> 
      <otherAttributes> 
      <name>Y</name> 
      <value>6</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>H</login> 
      <otherAttributes> 
      <name>Y</name> 
      <value>7</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>A</login> 
      <otherAttributes> 
      <name>Y</name> 
      <value>7</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
    </searchResponse> 
    </part> 
</SearchReply_OutputVariable> 

Erwartete XML-Ausgabe:

<SearchReply_OutputVariable> 
    <part name="parameters"> 
    <searchResponse> 
     <userSearchRecords> 
     <userAccount> 
      <login>A</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>1</value> 
      </otherAttributes> 
      <otherAttributes> 
      <name>Y</name> 
      <value>7</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>B</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>2</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>C</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>1</value> 
      </otherAttributes> 
      <otherAttributes> 
      <name>Y</name> 
      <value>6</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>D</login> 
      <otherAttributes> 
      <name>X</name> 
      <value>1</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>G</login> 
      <otherAttributes> 
      <name>Y</name> 
      <value>5</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
     <userSearchRecords> 
     <userAccount> 
      <login>H</login> 
      <otherAttributes> 
      <name>Y</name> 
      <value>7</value> 
      </otherAttributes> 
     </userAccount> 
     </userSearchRecords> 
    </searchResponse> 
    </part> 
</SearchReply_OutputVariable> 

In der XML-Ausgabe gibt es nun 6 <userSearchRecords>, während in der XML-Eingabe In der XML-Eingabe gibt es 8 waren waren 2 <userSearchRecords> mit <login> Wert A, und 2 <userSearchRecords> mit <login> Wert C. In der Ausgabe XML wurden diese Elemente zusammengeführt (<otherAttributes> Kind aus der übereinstimmenden logi verschoben n Paar und <userSearchRecords> Element aus dem passenden Paar entfernt).

Kann mir jemand in die richtige Richtung zeigen, wie man das löst?

Danke!

+1

Es ist eine Gruppe Problem. Mit XSLT 2.0 können Sie '' verwenden, mit XSLT 1.0 verwenden Sie eine muenchische Gruppierung mit einem Schlüssel ' '. –

Antwort

0

Danke Martin, schaute ich nach oben in Bezug auf xsl ‚Gruppierung‘ und das ist, was ich als Lösung kam:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       exclude-result-prefixes="xsl"> 
<xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
</xsl:template> 

    <xsl:key name="group" match="userSearchRecords" use="userAccount/login"/> 

<xsl:template match="searchResponse"> 
    <searchResponse> 
     <xsl:for-each select="userSearchRecords[count(. | key('group', userAccount/login)[1]) = 1]"> 
     <xsl:sort select="userAccount/login" /> 
     <userSearchRecords> 
      <userAccount> 
       <xsl:for-each select="key('group', userAccount/login)"> 
        <xsl:if test="not(preceding-sibling::userSearchRecords[userAccount/login = current()/userAccount/login])"> 
         <xsl:copy-of select="userAccount/login"/> 
        </xsl:if>    
        <xsl:copy-of select="userAccount/otherAttributes"/> 
       </xsl:for-each> 
      </userAccount> 
     </userSearchRecords> 
    </xsl:for-each> 
    </searchResponse> 
    </xsl:template> 
</xsl:stylesheet>