2011-05-05 43 views
0

我有一個複雜的xml。 (這裏只顯示了一些部分)。我的XSLT可以很簡單地顯示以下輸出。爲所列XML編寫XSLT的簡單方法

輸入XML

<?xml version="1.0" encoding="UTF-8"?> 
<FINALDATA CPCreatedDate_UTC="2012-4-3 16:45:36.187"> 
<SOLUTIONS> 
    <PROBLEMCAUSE Key="41607CD1-1B0F-45FA-B0DF-FACBCBBFCD19">thermocouple harness 
     <SOLUTION> 
      <ID>20600000000000000000000000000001</ID> 
      <MESSEAGE Key="1342F86E-6D34-429D-AE09-C71DB1933206">Broken wire</MESSEAGE> 
     </SOLUTION> 
    </PROBLEMCAUSE> 
    <PROBLEMCAUSE Key="7C6534BE-CEA5-4BD7-8C38-C2D51B72FA42">thermocouple data 
     <SOLUTION> 
      <ID>20600000000000000000000000000002</ID> 
      <MESSEAGE Key="DCA8E42A-BCD2-431F-87FA-9485F1B23C9E">Pump Value</MESSEAGE> 
     </SOLUTION> 
    </PROBLEMCAUSE> 
</SOLUTIONS> 
</FINALDATA> 

輸出XML(必需)

<Delta> 
<Package Time = "2012-4-3 16:45:36.187" > 
     <Entities> 
     <FailDatas> 
      <FailData>thermocouple harness</FailData> 
      <FailData>thermocouple data</FailData> 
     </FailDatas> 
     <Messages> 
      <Message>Broken wire</Message> 
      <Message>Pump Value</Message> 
     </Messages> 
    </Entities> 
    <Relationships> 
    </Relationships> 
    <Relationships></Relationships> 
</Package> 
</Delta> 

這一切我都得基於上面列出的鍵值做。請幫助我形成這一點。

感謝 拉姆

+1

你有什麼這麼遠嗎? – rsp 2011-05-05 09:58:54

+1

在你的輸出中''應該是''的孩子,還是你真的想把它當兄弟姐妹? – 2011-05-05 10:55:37

+1

輸出中的「」元素對應於什麼? – 2011-05-05 11:07:49

回答

1
<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes" /> 

    <xsl:template match="*"/> 

    <xsl:template match="FINALDATA"> 
     <Delta> 
       <Package Time="{@CPCreatedDate_UTC}"> 
      <xsl:apply-templates select="@*|node()" /> 
       </Package> 
     </Delta> 
    </xsl:template> 

    <xsl:template match="SOLUTIONS"> 
     <Entities> 
      <FailDatas> 
       <xsl:apply-templates select="PROBLEMCAUSE"/> 
      </FailDatas> 
      <Messages> 
       <xsl:apply-templates select="PROBLEMCAUSE/SOLUTION/MESSEAGE"/> 
      </Messages> 
      <xsl:apply-templates select="PROBLEMCAUSE" mode="Relationships" /> 
     </Entities> 
    </xsl:template> 

    <xsl:template match="PROBLEMCAUSE"> 
     <FailData> 
      <xsl:value-of select="normalize-space(text())"/> 
     </FailData> 
    </xsl:template> 

    <xsl:template match="MESSEAGE"> 
     <Message> 
      <xsl:apply-templates /> 
     </Message> 
    </xsl:template> 

    <xsl:template match="PROBLEMCAUSE" mode="Relationships"> 
     <Relationships /> 
    </xsl:template> 

</xsl:stylesheet>