XSL 将子元素复制到父属性
XSL Copy Child Element to Parent Attribute
我刚刚开始了解 XLS,只需修改下面的 XML。特别是,我想复制 <description>
元素的值并将其替换到其父 <game>
.
的 name
属性中
来源XML:
<?xml version="1.0"?>
<menu>
<game name="0000P" index="" image="">
<description>0,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="0,000 Pyramid (1988)" index="" image="">
<description>0,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>
您的方法存在问题,当您这样做时:
<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>
我刚刚开始了解 XLS,只需修改下面的 XML。特别是,我想复制 <description>
元素的值并将其替换到其父 <game>
.
name
属性中
来源XML:
<?xml version="1.0"?>
<menu>
<game name="0000P" index="" image="">
<description>0,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="0,000 Pyramid (1988)" index="" image="">
<description>0,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>
您的方法存在问题,当您这样做时:
<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>