2015-09-18 62 views
1

我有這樣的XML,XSLT - 獲得原始屬性到結果樹

<doc> 
    <section type="Main_Content"> 
     <p id="main">aa</p> 
     <p id="main">bb</p> 
     <p id="main">cc</p> 
     <p id="para1">dd</p> 
     <p id="main">ee</p> 
     <p id="main">ff</p> 
     <p id="para2">hh</p> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 

    </section> 
    <section type="Main_Chapter"> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
     <p id="main">zz</p>  
    </section> 
</doc> 

我的任務是小組根據ID上述內容=「PARA1」和id =「PARA2」的屬性,並添加一個節到每個組。我期望的輸出是

<doc> 
    <section type="Main_Content"> 
     <p id="main">aa</p> 
     <p id="main">bb</p> 
     <p id="main">cc</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">dd</p> 
     <p id="main">ee</p> 
     <p id="main">ff</p> 
    </section> 
    <section type="Second para"> 
     <p id="para2">hh</p> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
    </section> 
    <section type="Main_Chapter"> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
     <p id="main">zz</p> 
    </section> 
</doc> 

的XSL代碼這個任務,

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

    <xsl:template match="section"> 
     <xsl:for-each-group select="p" group-starting-with="p[starts-with(@id, 'para')]"> 
      <section type="{@type}"> 
       <xsl:if test="current-group()[1][@id='para1']"> 
        <xsl:attribute name="type" select="'First para'"/> 
       </xsl:if> 
       <xsl:if test="current-group()[1][@id='para2']"> 
        <xsl:attribute name="type" select="'Second para'"/> 
       </xsl:if> 
       <xsl:apply-templates select="current-group()"/> 
      </section>  
     </xsl:for-each-group> 
    </xsl:template> 

結果從上面的XSL得到的是,

<doc> 
    <section type=""> 
     <p id="main">aa</p> 
     <p id="main">bb</p> 
     <p id="main">cc</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">dd</p> 
     <p id="main">ee</p> 
     <p id="main">ff</p> 
    </section> 
    <section type="Second para"> 
     <p id="para2">hh</p> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
    </section> 
    <section type=""> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
     <p id="main">zz</p> 
    </section> 
</doc> 

SO,因爲它似乎在結果除了原來的type屬性沒有被複制到<section>節點外,其它情況都是正確的。如何修改xsl以獲得原始type屬性值到<section>節點?

回答

4

的問題是在所提供的這部分代碼:

<xsl:template match="section"> 
    <xsl:for-each-group select="p" group-starting-with="p[starts-with(@id, 'para')]"> 
     <section type="{@type}"> 

執行通知該上下文(電流)項是一個p元件,並且沒有這些提供的源XML文檔中有一個type屬性。

這裏您需要p元素的父項的type屬性

解決方案

替換:

 <section type="{@type}"> 

有:

 <section type="{../@type}"> 

現在整個改造成爲

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

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

<xsl:template match="section"> 
    <xsl:for-each-group select="p" group-starting-with="p[starts-with(@id, 'para')]"> 
     <section type="{../@type}"> 
     <xsl:if test="current-group()[1][@id='para1']"> 
      <xsl:attribute name="type" select="'First para'"/> 
     </xsl:if> 
     <xsl:if test="current-group()[1][@id='para2']"> 
      <xsl:attribute name="type" select="'Second para'"/> 
      </xsl:if> 
      <xsl:apply-templates select="current-group()"/> 
     </section>  
    </xsl:for-each-group> 
</xsl:template> 
</xsl:stylesheet> 

,並且當所提供的源XML文檔施加:

<doc> 
    <section type="Main_Content"> 
     <p id="main">aa</p> 
     <p id="main">bb</p> 
     <p id="main">cc</p> 
     <p id="para1">dd</p> 
     <p id="main">ee</p> 
     <p id="main">ff</p> 
     <p id="para2">hh</p> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
    </section> 
    <section type="Main_Chapter"> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
     <p id="main">zz</p> 
    </section> 
</doc> 

的想要的,正確的結果產生:

<doc> 
    <section type="Main_Content"> 
     <p id="main">aa</p> 
     <p id="main">bb</p> 
     <p id="main">cc</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">dd</p> 
     <p id="main">ee</p> 
     <p id="main">ff</p> 
    </section> 
    <section type="Second para"> 
     <p id="para2">hh</p> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
    </section> 
     <section type="Main_Chapter"> 
     <p id="main">ii</p> 
     <p id="main">jj</p> 
    </section> 
    <section type="First para"> 
     <p id="para1">xx</p> 
     <p id="main">yy</p> 
     <p id="main">zz</p> 
    </section> 
</doc>