2017-03-01 42 views
0

我有一個xml,它是幾個子系統查詢的合併輸出。現在我必須選擇很多信息。例如:在特定條件下的XSLT 1.0轉換

/合併/響應/ BenutzerDaten/Deaktiviert

如果/合併/響應/結果/結果/ DN結尾 「O =測試中,c =德」 和只有一個結尾結果圖案:

/合併/響應/結果/結果/屬性/ identnummer

如果/合併/響應/結果/結果/ DN與 「DC = TEST,DC = DE」 結束:

/Merge/response/results/result/attributes/postalCode

最好的辦法是做什麼?

這裏是我的索裏XML的減少樣本:

<?xml version="1.0" encoding="UTF-8"?> 
<Merge> 
    <response> 
     <results> 
      <result> 
       <dn>cn=user1,ou=members,ou=group1,o=test,c=de</dn> 
       <attributes> 
        <identnummer>re3232</identnummer> 
        <uid>user1</uid> 
        [...] 
        <adminuid>admin3</adminuid> 
       </attributes> 
      </result> 
     </results> 
    </response> 
    <response> 
     <results> 
      <result> 
       <dn>CN=user1,OU=Users,DC=TEST,DC=DE</dn> 
       <attributes> 
        <postalCode>12345</postalCode> 
        <company>My Company</company> 
        [...] 
        <streetAddress>XSLT Road 1</streetAddress> 
       </attributes> 
      </result> 
     </results> 
    </response> 
    <response> 
     <secDN>principalName=user1,cn=Users,secDomain=Default</secDN> 
     <Benutzerdaten> 
      <Deaktiviert>false</Deaktiviert> 
      <Gesperrt>false</Gesperrt> 
      [...] 
      <PasswortGueltig>true</PasswortGueltig> 
     </Benutzerdaten> 
    </response> 
    <Nutzdaten> 
     <Identifikation> 
      <AdminID>admin3</AdminID> 
      <BenutzerID>user1</BenutzerID> 
     </Identifikation> 
     <Benutzerdaten> 
      <Datentyp>Account-Daten</Datentyp> 
      <Datentyp>Anwendungs-Daten</Datentyp> 
      <Datentyp>Benutzer-Daten</Datentyp> 
     </Benutzerdaten> 
    </Nutzdaten> 
</Merge> 

預期的結果應該是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<UserData> 
    <user>user1</user> 
    <source1> 
     <identnummer>re3232</identnummer> 
    </source1> 
    <source2> 
     <postalCode>12345</postalCode> 
    </source2> 
    <source3> 
     <Deaktiviert>false</Deaktiviert> 
    </source3> 
</UserData> 

這裏是我的失敗嘗試:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <xsl:choose> 
     <xsl:when test="/Merge/response/Benutzerdaten and count(/Merge/response/results/result[dn = '*,o=test,c=de') = 1" and count(/Merge/response/results/result[dn = '*,DC=TEST,DC=DE') = 1" > 
     <UserData> 
      <user> 
      <xsl:value-of select="/Merge/Nutzdaten/Identifikation/BenutzerID"/> 
      </user> 
      <xsl:if test="/Merge/response/Benutzerdaten/*"> 
      <Account> 
       <xsl:copy-of select="/Merge/response/Benutzerdaten/*"/> 
      </Account> 
      </xsl:if> 
      <xsl:if test="substring(/Merge/response/results/result/dn, string-length(/Merge/response/results/result/dn) -11) = ',o=test,c=de'"> 
      <source1> 
       <postalCode> 
        <xsl:value-of select="/Merge/response/results/result/attributes/portalCode"></xsl:value-of> 
       </postalCode> 
      </source1> 
      </xsl:if> 
      <xsl:if test="substring(/Merge/response/results/result/dn, string-length(/Merge/response/results/result/dn) -12) = ',DC=TEST,DC=DE'"> 
      <source2> 
       <postalCode> 
        <xsl:value-of select="/Merge/response/results/result/attributes/portalCode"></xsl:value-of> 
       </postalCode> 
      </source2> 
      </xsl:if> 
      <source3> 
      <Deaktiviert> 
       <xsl:value-of select="/Merge/response/BenutzerDaten/Deaktiviert"></xsl:value-of> 
      </Deaktiviert> 
      </source3> 
     </UserData> 
     </xsl:when> 
     <xsl:otherwise> 
     <response> 
      <failure> 
      <xsl:choose> 
       <xsl:when test="count(/Merge/response/results/result[dn = '*,o=test,c=de') > 1"> 
       <message>more than one result from source1</message> 
       </xsl:when> 
       <xsl:otherwise> 
       <message>The requested object does not exist.</message> 
       </xsl:otherwise> 
      </xsl:choose> 
      </failure> 
     </response> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

回答

0

嘗試如下:

<xsl:param name="text" select="/Merge/response/results/result/dn[contains(text(),'o=test,c=de')]"/> 
    <xsl:param name="text1" select="/Merge/response/results/result/dn[contains(text(),'DC=TEST,DC=DE')]"/> 
    <xsl:choose> 
     <xsl:when test="/Merge/response/Benutzerdaten and count(/Merge/response/results/result/dn [contains(text(),'o=test,c=de')]) = 1 and count(/Merge/response/results/result/dn[contains(text(),'DC=TEST,DC=DE')]) = 1" > 
      <UserData> 
       <user> 
        <xsl:value-of select="/Merge/Nutzdaten/Identifikation/BenutzerID"/> 
       </user> 
       <xsl:if test="/Merge/response/Benutzerdaten/*"> 
        <Account> 
         <xsl:copy-of select="/Merge/response/Benutzerdaten/*"/> 
        </Account> 
       </xsl:if> 
       <xsl:if test="substring($text, string-length($text)-11)"> 
        <source1> 
         <postalCode> 
          <xsl:value-of select="/Merge/response/results/result/attributes/portalCode"></xsl:value-of> 
         </postalCode> 
        </source1> 
       </xsl:if> 
       <xsl:if test="substring($text, string-length($text)-12)"> 
        <source2> 
         <postalCode> 
          <xsl:value-of select="/Merge/response/results/result/attributes/postalCode"></xsl:value-of> 
         </postalCode> 
        </source2> 
       </xsl:if> 
       <source3> 
        <Deaktiviert> 
         <xsl:value-of select="/Merge/response/Benutzerdaten/Deaktiviert"></xsl:value-of> 
        </Deaktiviert> 
       </source3> 
      </UserData> 
     </xsl:when> 
     <xsl:otherwise> 
      <response> 
       <failure> 
        <xsl:choose> 
         <xsl:when test="count(/Merge/response/results/result/dn[contains(text(),'o=test,c=de')]) > 1"> 
          <message>more than one result from source1</message> 
         </xsl:when> 
         <xsl:when test="count(/Merge/response/results/result/dn[contains(text(),'DC=TEST,DC=DE')]) > 1"> 
          <message>more than one result from source2</message> 
         </xsl:when> 
         <xsl:otherwise> 
          <message>The requested object does not exist.</message> 
         </xsl:otherwise> 
        </xsl:choose> 
       </failure> 
      </response> 
     </xsl:otherwise> 
    </xsl:choose>