2011-05-29 83 views
2

HI變換特定的XML文檔用XSLT

我的XML文檔具有以下佈局

<root> 
    <child> 
    <item type="ID">id1</item> 
    <item type="TEXT">text1</item> 
    </child> 

    <child> 
    <item type="ID"/> 
    <item type="TEXT">text2</item> 
    </child> 

    <child> 
    <item type="ID"/> 
    <item type="TEXT">text3</item> 
    </child> 

    <child> 
    <item type="ID">id2</item> 
    <item type="TEXT">text4</item> 
    </child> 

    <child> 
    <item type="ID"/> 
    <item type="TEXT">text5</item> 
    </child> 

    ... 
</root> 

每次存在與類型= ID的空項目標籤,這意味着它具有相同的值作爲在兄弟姐妹之前。現在我想變成

<root> 
    <child id="id1">text1text2text3</child> 
    <child id="id2">text4text5</child> 

    ... 
</root> 

我解決這,這是

<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/"> 
    <root> 
     <xsl:apply-templates select="//child/item[@type='ID'][text()='id1' or text()='id2']"/> 
    </root> 
    </xsl:template> 

    <xsl:template match="child/item[text()='id1' or text()='id2']"> 
    <child id="{text()}"> 
     <xsl:value-of select="./../item[@type='TEXT']/."/> 
     <xsl:apply-templates select="./../following::*[1]/item[@type='ID'][not(text()='id1' or text()='id2')]"/> 
    </child> 
    </xsl:template> 

    <xsl:template match="child/item[not(text()='id1' or text()='id2')]"> 
    <xsl:value-of select="./../item[@type='TEXT']/."/> 
    <xsl:apply-templates select="./../following::*[1]/item[@type='ID'][not(text()='id1' or text()='id2')]"/> 
    </xsl:template> 

</xsl:stylesheet> 

它的工作原理,但很醜陋,不易伸縮。例如,如果我有任意的id值,而不僅僅是id1和id2。有人有好的建議或更好的解決方案嗎?

+0

您可以使用XSLT 2還是僅限於XSLT 1.0? – 2011-05-29 20:45:25

+0

好問題,+1。使用密鑰查看我的答案,獲得完整,簡單易用的XSLT 1.0解決方案。 – 2011-05-30 00:56:38

回答

1

這XSLT 1.0樣式表應該這樣做:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output indent="yes"/> 

    <xsl:template match="/*"> 
     <root> 
      <!-- visit each "child" element with a non-blank ID --> 
      <xsl:for-each select="child[item[@type='ID'] != '']"> 
       <xsl:variable name="this-id" select="item[@type='ID']"></xsl:variable> 
       <child id="{$this-id}"> 
        <!-- visit this node and each following "child" element which 
         1. has a blank ID, and 
         2. whose immediate preceding non-blank ID child element is this one -->       
        <xsl:for-each select=".|following-sibling::child 
         [item[@type='ID'] = ''] 
         [preceding-sibling::child[item[@type='ID'] != ''][1]/item[@type='ID']=$this-id]"> 
         <xsl:value-of select="item[@type='TEXT']"/> 
        </xsl:for-each> 
       </child> 

      </xsl:for-each> 
     </root> 
    </xsl:template>  

</xsl:stylesheet> 

1

XSLT 2.0溶液:

<xsl:template match="root"> 
    <xsl:for-each-group select="child" 
         group-starting-with="child[string(item[@type='ID'])]"> 
    <child id="{item[@type='ID']}"> 
     <xsl:value-of select="current-group()/item[@type='TEXT']" separator=""/> 
    </child> 
    </xsl:for-each-group> 
</xsl:template> 
1

這XSLT 1.0轉化

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:key name="kFollowing" match="item[@type='TEXT']" 
     use="((..|../preceding-sibling::child) 
         /item[@type='ID'and string(.)] 
      )[last()]"/> 

<xsl:template match="/*"> 
    <root> 
    <xsl:apply-templates select= 
     "*[item[@type='ID' and string(.)]]"/> 
    </root> 
</xsl:template> 

<xsl:template match="*"> 
    <child id="{item[@type='ID']}"> 
    <xsl:copy-of select="key('kFollowing', item[@type='ID'])/text()"/> 
    </child> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的應用XML文檔

<root> 
    <child> 
     <item type="ID">id1</item> 
     <item type="TEXT">text1</item> 
    </child> 
    <child> 
     <item type="ID"/> 
     <item type="TEXT">text2</item> 
    </child> 
    <child> 
     <item type="ID"/> 
     <item type="TEXT">text3</item> 
    </child> 
    <child> 
     <item type="ID">id2</item> 
     <item type="TEXT">text4</item> 
    </child> 
    <child> 
     <item type="ID"/> 
     <item type="TEXT">text5</item> 
    </child> ... 
</root> 

產生想要的,正確的結果

<root> 
    <child id="id1">text1text2text3</child> 
    <child id="id2">text4text5</child> 
</root> 

說明:使用鍵來表達於第一前item@type='ID'和所有匹配item元件之間的關係子文本節點。