【问题标题】:XSL Copy Child Element to Parent AttributeXSL 将子元素复制到父属性
【发布时间】:2015-07-26 03:42:15
【问题描述】:

我刚刚开始学习 XLS,只需在下面修改我的 XML。特别是,我想复制<description> 元素的值并将其替换为其父<game>name 属性。

源 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>

【问题讨论】:

    标签: xslt attributes parent-child elements


    【解决方案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>
    

    【讨论】:

    • 感谢您的回复。我尝试使用两个建议的 xls 模板,结果是:
    • $100,000 Pyramid (1988) Box Office, Inc. 1988 策略是 High Stakes by Dick Francis (1986) Mindscape, Inc. 1986 Adventure 是 007 - 杀人许可证 (1989) Domark Ltd. 1989 驾驶是的
    • 它现在将描述元素复制到游戏元素的名称属性中。然而,游戏元素下的所有其他元素似乎都消失了。
    • @D.Sync 您需要保持身份转换模板(样式表中的第一个模板)不变。在此处查看演示:xsltransform.net/jyH9rMM
    • 啊,我一定错过了那个!现在可以正常输出了!感谢@michael.hor257k 的帮助。我刚刚开始学习 XLS,希望这将是一个好的开始 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    相关资源
    最近更新 更多