2011-05-29 82 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元件之间的关系子文本节点。