【问题标题】:An attribute node cannot be created after the children of the containing element不能在包含元素的子元素之后创建属性节点
【发布时间】:2014-04-24 10:23:38
【问题描述】:

我有一个简单的 XML 文件,我需要为它更新一个属性值(应该足够简单)。尽管复制了其他有效的示例,但我收到错误消息“无法在包含元素的子元素之后创建属性节点 (LastUpdateDate)”。谁能发现我做错了什么或提出替代方法?

我的 XML 是:

<Root>
  <Datasource Name="SCV">
    <Datafeed FeedMask="yyyyMMdd_Individual" LastUpdateDate="20140401"/>
    <Datafeed FeedMask="yyyyMMdd_MarketingPreferences" LastUpdateDate="20140401"/>
  </Datasource>
</Root>

我的 XSL 是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/Root/Datasource[@Name='SCV']/Datafeed[@FeedMask='yyyyMMdd_Individual']">
    <xsl:attribute name="LastUpdateDate"><xsl:value-of select="20140402"/> </xsl:attribute>
  </xsl:template>
</xsl:transform>

感谢您的帮助,

史蒂夫

【问题讨论】:

    标签: xslt


    【解决方案1】:

    您需要先复制元素,然后才能为其添加属性:

    <xsl:template match="/Root/Datasource[@Name='SCV']/Datafeed[@FeedMask='yyyyMMdd_Individual']">
        <xsl:copy>
            <xsl:attribute name="LastUpdateDate">
                <xsl:value-of select="20140402"/>
            </xsl:attribute>
        </xsl:copy>
    </xsl:template>
    

    注意,如果属性已经存在,而你只想修改它,你可以让模板直接匹配属性:

    <xsl:template match="/Root/Datasource[@Name='SCV']/Datafeed[@FeedMask='yyyyMMdd_Individual']/@LastUpdateDate">
        <xsl:attribute name="LastUpdateDate">
            <xsl:value-of select="20140402"/>
        </xsl:attribute>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 2014-12-05
      • 1970-01-01
      • 2021-06-14
      • 2021-09-05
      相关资源
      最近更新 更多