2010-09-02 105 views
1

我有一個XMLXSLT分組問題

<Root> 
    <Parent> 
    <Child1><Node1>AAA</Node1><Node2>BBB</Node2></Child1> 
    <Child2><NodeX>XXX</NodeX><NodeY>YYY</NodeY></Child2> 
    <Child1><Node1>EEE</Node1><Node2>FFF</Node2></Child1> 
    <Child2><NodeX>GGG</NodeX><NodeY>HHH</NodeY></Child2> 
    <OtherChild></OtherChild> 

    </Parent> 
</Root> 

CHILD2總是將與child1。我需要知道如何循環使用xsl:foreach並創建一個XML輸出示例。我可以有一個像<OtherChild>其他節點,但我關心的 只有Child1和Chid2節點

<TransformedXML> 
    <Child attributefromNode1="AAA" attributefromNode2="BBB" attributefromNodeX="XXX" attributeFromNodeY="YYY"/> 
    <Child attributefromnode1="EEE" attributefromNode2="FFF" attributefromNodeX="GGG" attributeFromNodeY="HHH"/> 
</TransformedXML> 

我的問題是我怎麼在XSLT考慮CHILD2節點將遵循Child1總是循環?

+1

提示:當您要編輯一職,請按問題底部的「編輯」鏈接,不要使用瀏覽器的後退按鈕。否則,你會再次重新發布相同的問題。這是你的原創:http://stackoverflow.com/questions/3623606/xslt-grouping-question – BalusC 2010-09-02 03:15:42

+0

我很驚訝所以沒有抓住這個雙重職位。 – deceze 2010-09-02 03:17:35

+0

@deceze它做到了。或者,相反,SO用來捕捉欺騙問題的機制確實如此。這是社區。 – Will 2010-09-02 11:24:31

回答

0

我這這樣的事情應該做的伎倆:

<xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns="http://www.w3.org/TR/xhtml1/strict"> 
    <xsl:output 
    method="xml" 
    indent="yes" /> 

    <xsl:template match="/"> 
<xsl:for-each select="Root/Parent/Child1"> 
<Child> 
    <xsl:attribute name="attributefromNode1"> 
    <xsl:value-of select="Node1" /> 

    </xsl:attribute> 
    <xsl:attribute name="attributefromNode2" > 
    <xsl:value-of select="Node2" /> 

    </xsl:attribute> 
    <xsl:attribute name="attributefromNodeX"> 
    <xsl:value-of select="following-sibling::Child2/NodeX" /> 

    </xsl:attribute> 
    <xsl:attribute name="attributefromNodeY"> 
    <xsl:value-of select="following-sibling::Child2/NodeY" /> 

    </xsl:attribute> 



</Child> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 
+0

如果您要硬編碼元素和屬性名稱,爲什麼您不會使用文字結果元素和屬性值模板作爲DevNull答案?這是硬編碼和詳細的。 – 2010-09-03 14:49:53

0

你不應該有環......

XML輸入

<Root> 
    <Parent> 
    <Child1><Node1>AAA</Node1><Node2>BBB</Node2></Child1> 
    <Child2><NodeX>XXX</NodeX><NodeY>YYY</NodeY></Child2> 
    <Child1><Node1>EEE</Node1><Node2>FFF</Node2></Child1> 
    <Child2><NodeX>GGG</NodeX><NodeY>HHH</NodeY></Child2> 
    <OtherChild></OtherChild> 

    </Parent> 
</Root> 

XSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

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

    <xsl:template match="Child1"> 
    <Child attributefromNode1="{Node1}" 
     attributefromNode2="{Node2}" 
     attributefromNodeX="{following-sibling::Child2[1]/NodeX}" 
     attributefromNodeY="{following-sibling::Child2[1]/NodeY}"/> 
    </xsl:template> 

</xsl:stylesheet> 

XML OUTPUT

<?xml version="1.0" encoding="UTF-8"?> 
<TransformedXML> 
    <Child attributefromNode1="AAA" attributefromNode2="BBB" attributefromNodeX="XXX" 
      attributefromNodeY="YYY"/> 
    <Child attributefromNode1="EEE" attributefromNode2="FFF" attributefromNodeX="GGG" 
      attributefromNodeY="HHH"/> 
</TransformedXML> 
0

這裏是(幾乎與前面相同)短溶液

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="kFollowingChild1" match="*[not(self::Child1)]/*" 
    use="generate-id(../preceding-sibling::Child1[1])"/> 

<xsl:template match="Parent"> 
    <TransformedXML> 
    <xsl:apply-templates/> 
    </TransformedXML> 
</xsl:template> 

<xsl:template match="Child1"> 
    <Child> 
    <xsl:for-each select="*|key('kFollowingChild1', generate-id())"> 
    <xsl:attribute name="attribute{position()}"> 
     <xsl:value-of select="."/> 
    </xsl:attribute> 
    </xsl:for-each> 
    </Child> 
</xsl:template> 

<xsl:template match="text()"/> 
</xsl:stylesheet> 

當該變換被應用到所提供的XML文檔

<Root> 
    <Parent> 
    <Child1><Node1>AAA</Node1><Node2>BBB</Node2></Child1> 
    <Child2><NodeX>XXX</NodeX><NodeY>YYY</NodeY></Child2> 
    <Child1><Node1>EEE</Node1><Node2>FFF</Node2></Child1> 
    <Child2><NodeX>GGG</NodeX><NodeY>HHH</NodeY></Child2> 
    <OtherChild></OtherChild> 
    </Parent> 
</Root> 

想要的,正確的結果產生

<TransformedXML> 
    <Child attribute1="AAA" attribute2="BBB" attribute3="XXX" attribute4="YYY"/> 
    <Child attribute1="EEE" attribute2="FFF" attribute3="GGG" attribute4="HHH"/> 
</TransformedXML> 
0

這XSLT 2.0樣式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="Parent"> 
     <TransformedXML> 
      <xsl:apply-templates select="Child1"/> 
     </TransformedXML> 
    </xsl:template> 
    <xsl:template match="Child1"> 
     <Child> 
      <xsl:apply-templates select="*|following-sibling::Child2[1]/*"/> 
     </Child> 
    </xsl:template> 
    <xsl:template match="Child1/*|Child2/*"> 
     <xsl:attribute name="attributefrom{name()}"> 
      <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<TransformedXML> 
    <Child attributefromNode1="AAA" attributefromNode2="BBB" attributefromNodeX="XXX" attributefromNodeY="YYY"/> 
    <Child attributefromNode1="EEE" attributefromNode2="FFF" attributefromNodeX="GGG" attributefromNodeY="HHH"/> 
</TransformedXML>