xml - XSLT - Add new attribute to existing node by checking existing attribute -


hi have following xml,

<doc>  <footnote>    <p type="footnote text">        <link ref="http://www.facebook.org"/>    </p>  </footnote> <footnote>    <p type="footnote text">        <link ref="http://www.wiki.org"/>    </p>  </footnote> <footnote>    <p type="footnote paragraph">        <link ref="http://www.wiki.org"/>    </p>  </footnote> </doc> 

what need add new attribute (id='number-1') <p> nodes attribute type eqal footnote text. in case first 2 <p> nodes should apply new <id> attribute.

so wrote following code,

<xsl:template match="p/@type[.='footnote text']">    <xsl:copy>       <xsl:attribute name="id">          <xsl:value-of select="'number-'"/><xsl:number level="any"/>       </xsl:attribute>       <xsl:apply-templates select="node()|@*"/>    </xsl:copy> </xsl:template> 

but not add new attribute <p> node. when remove @type[.='footnote text'] adds new node <p> nodes expected.

my intended output follows

<doc>    <footnote>       <p type="footnote text" id="number-1">           <link ref="http://www.facebook.org"/>       </p>    </footnote>    <footnote>      <p type="footnote text" id="number-2>         <link ref="http://www.wiki.org"/>      </p>    </footnote>    <footnote>      <p type="footnote paragraph">        <link ref="http://www.wiki.org"/>      </p>    </footnote>  </doc> 

how can filter <p> nodes have type="footnote text" attribute , add new attribute <p> nodes ?

you need add attribute element:

<xsl:template match="p[@type = 'footnote text']">    <xsl:copy>       <xsl:attribute name="id">          <xsl:value-of select="'number-'"/><xsl:number count="p[@type = 'footnote text']" level="any"/>       </xsl:attribute>       <xsl:apply-templates select="node()|@*"/>    </xsl:copy> </xsl:template> 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -