繁体   English   中英

使用xslt在给定位置的元素上添加属性

[英]Adding an attribute to an element in a given location using xslt

是否可以将新属性添加到已经包含其他属性但默认不为第一个属性的xml元素中。

例如,我有一个像这样的xml元素:

<datapoint type="Footnote" subtype="" name="SecurityCusipFootnote" display="always" />

我想添加一个名为“值”的属性。

如果我在样式表中使用此代码:

<xsl:copy>
  <xsl:attribute name="value">
    <xsl:value-of select="42"/>
  </xsl:attribute>
</xsl:copy>

然后转换为

<datapoint value="42" type="Footnote" subtype="" name="SecurityCusipFootnote" display="always" />

是否可以专门放置新属性,使其出现在现有属性之后,以便转换将其读取

<datapoint type="Footnote" subtype="" name="SecurityCusipFootnote" value="42" display="always" />

新的“值”属性出现在“名称”属性之后。

提前谢谢了。

这可能会或可能不会,取决于您的特定处理器决定如何处理任务:

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

<xsl:template match="datapoint/@name">
    <xsl:copy-of select="."/>
    <xsl:attribute name="value">
        <xsl:value-of select="42"/>
    </xsl:attribute>
</xsl:template>

为什么这完全是个问题? XML规范明确指出属性的顺序并不重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM