![](/img/trans.png)
[英]Adding a new element based on a condition using XSLT at a Specific Location
[英]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.