XSLT - 向节点添加递增id属性

时间:2015-07-08 10:14:23

标签: xml xslt xslt-2.0

我有一个像这样的xml,

<doc>
  <section>1<section>
  <section>1<section>
  <p>22</p>
  <p>22</p>
  <p>22</p>
  <footer></footer>
  <footer></footer>
  <footer></footer>
</doc>

我需要做什么id<footer>个节点添加新属性。 所以输出将是

<doc>
   <section>1<section>
   <section>1<section>
   <p>22</p>
   <p>22</p>
   <p>22</p>
   <footer id="number-1"></footer>
   <footer id="number-2"></footer>
   <footer id="number-3"></footer>
 </doc>

我可以将新属性添加到<footer&gt;节点,但我面临的问题是在XSLT中添加递增ID。

<xsl:template match="footer">
    <xsl:copy>
        <xsl:attribute name="id"><xsl:value-of select="'number-'[position()]"/></xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

我试过使用xsl变量,但因为它不能像其他语言那样改变我无法做到。我也尝试使用position()函数,但它只给出当前节点的位置。所以我的情况是从6开始的id数字。

你可以建议我一个解决方案吗?提前致谢

1 个答案:

答案 0 :(得分:2)

你可以使用

df.loc[df['Project ID'] == 'Project2', 'Cost'].values
相关问题