2015-05-14 31 views
0

我剛剛開始瞭解XLS,只是在下面修改了我的XML。特別是,我想複製<description>元素的值並將其替換爲其父項<game>name屬性。XSL將子元素複製到父項屬性

源XML:

<?xml version="1.0"?> 
<menu> 
    <game name="$100000P" index="" image=""> 
     <description>$100,000 Pyramid (1988)</description> 
     <cloneof></cloneof> 
     <crc></crc> 
     <manufacturer>Box Office, Inc.</manufacturer> 
     <year>1988</year> 
     <genre>Strategy</genre> 
     <rating></rating> 
     <enabled>Yes</enabled> 
    </game> 
    <game name="$takes" index="" image=""> 
     <description>High Stakes by Dick Francis (1986)</description> 
     <cloneof></cloneof> 
     <crc></crc> 
     <manufacturer>Mindscape, Inc.</manufacturer> 
     <year>1986</year> 
     <genre>Adventure</genre> 
     <rating></rating> 
     <enabled>Yes</enabled> 
    </game> 
    <game name="007Licen" index="" image=""> 
     <description>007 - Licence to Kill (1989)</description> 
     <cloneof></cloneof> 
     <crc></crc> 
     <manufacturer>Domark Ltd.</manufacturer> 
     <year>1989</year> 
     <genre>Driving</genre> 
     <rating></rating> 
     <enabled>Yes</enabled> 
    </game> 
... 

所需的輸出:

<?xml version="1.0"?> 
<menu> 
    <game name="$100,000 Pyramid (1988)" index="" image=""> 
     <description>$100,000 Pyramid (1988)</description> 
     <cloneof></cloneof> 
     <crc></crc> 
     <manufacturer>Box Office, Inc.</manufacturer> 
     <year>1988</year> 
     <genre>Strategy</genre> 
     <rating></rating> 
     <enabled>Yes</enabled> 
    </game> 
    <game name="High Stakes by Dick Francis (1986)" index="" image=""> 
     <description>High Stakes by Dick Francis (1986)</description> 
     <cloneof></cloneof> 
     <crc></crc> 
     <manufacturer>Mindscape, Inc.</manufacturer> 
     <year>1986</year> 
     <genre>Adventure</genre> 
     <rating></rating> 
     <enabled>Yes</enabled> 
    </game> 
    <game name="007 - Licence to Kill (1989)" index="" image=""> 
     <description>007 - Licence to Kill (1989)</description> 
     <cloneof></cloneof> 
     <crc></crc> 
     <manufacturer>Domark Ltd.</manufacturer> 
     <year>1989</year> 
     <genre>Driving</genre> 
     <rating></rating> 
     <enabled>Yes</enabled> 
    </game> 

我曾嘗試以下XSL,但它不似乎做任何改變。現在真的抓我的頭了。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="game"> 
     <game name="{description}"> 
      <xsl:apply-templates select="@*|node()"/> 
     </game> 
    </xsl:template> 
</xsl:stylesheet> 

回答

1

與方法的問題是,當你做:

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

也複製原始@name屬性,覆蓋新@name屬性您剛纔創建的。

嘗試,而不是:

<xsl:template match="game"> 
    <game> 
     <xsl:copy-of select="@*"/> 
     <xsl:attribute name="name"> 
      <xsl:value-of select="description"/> 
     </xsl:attribute> 
     <xsl:apply-templates select="node()"/> 
    </game> 
</xsl:template> 

,或者,如果你知道所有屬性的game將有:

<xsl:template match="game"> 
    <game name="{description}" index="{@index}" image="{@image}"> 
     <xsl:apply-templates select="node()"/> 
    </game> 
</xsl:template> 
+0

感謝您的答覆。我嘗試使用兩個建議的xls模板,結果是: –

+0

<?xml version =「1.0」?> $ 100,000金字塔(1988) 票房公司策略 是 <遊戲名稱= 「高籌碼由迪克弗朗西斯(1986)」 索引= 「」 形象= 「」> 高迪克錦標弗朗西斯(1986) Mindscape,Inc. 冒險 是 <遊戲名稱= 「007 - 執照殺害(1989)」 索引= 「」 形象= 「」> 007 - 執照殺害(1989) Domark有限公司駕駛 是 –

+0

它現在將description元素複製到遊戲元素的name屬性中。然而,遊戲元素下的所有其他元素似乎只是消失了。 –

相關問題