Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt how to add attributes to copy-of

Tags:

xslt

I have the following piece of code in my XSLT file:

<xsl:copy-of select="/root/Algemeen/foto/node()" />

In the XML file the node /root/Algemeen/foto/ holds an HTML image, for example: <img src="somephoto.jpg" />

What I would like to do is to add a fixed width to the image. But the following doesn't work:

<xsl:copy-of select="/root/Algemeen/foto/node()">
    <xsl:attribute name="width">100</xsl:attribute>
</xsl:copy-of>
like image 408
Jules Colle Avatar asked Jun 04 '10 09:06

Jules Colle


People also ask

How do I copy nodes in XSLT?

XSLT <xsl:copy-of> The <xsl:copy-of> element creates a copy of the current node. Note: Namespace nodes, child nodes, and attributes of the current node are automatically copied as well! Tip: This element can be used to insert multiple copies of the same node into different places in the output.

How does xsl copy work?

Definition and Usage. The <xsl:copy> element creates a copy of the current node. Note: Namespace nodes of the current node are automatically copied as well, but child nodes and attributes of the current node are not automatically copied!

What is current () XSLT?

XSLT current() Function The current() function returns a node-set that contains only the current node. Usually the current node and the context node are the same.

What is TD in XSLT?

Definition and Usage The <td> tag defines a standard data cell in an HTML table. An HTML table has two kinds of cells: Header cells - contains header information (created with the <th> element) Data cells - contains data (created with the <td> element)


1 Answers

xsl:copy-of performs a deep copy of the selected node, but doesn't provide an opportunity to alter it.

You will want to use xsl:copy and then add additional nodes inside. xsl:copy just copies the node and namespace attributes, but not the regular attributes and child nodes, so you will want to ensure that you apply-templates to push the other nodes through as well. xsl:copy does not have a @select, it works on the current node, so wherever you were applying the <xsl:copy-of select="/root/Algemeen/foto/node()" /> , you will need to change to <xsl:apply-templates select="/root/Algemeen/foto/node()" /> and move the img logic into a template.

Something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="/">         <result>     <xsl:apply-templates select="/root/Algemeen/foto/img"/>         </result>     </xsl:template>  <!--specific template match for this img -->     <xsl:template match="/root/Algemeen/foto/img">       <xsl:copy>             <xsl:attribute name="width">100</xsl:attribute>             <xsl:apply-templates select="@*|node()" />           </xsl:copy>     </xsl:template>  <!--Identity template copies content forward -->     <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>     </xsl:template>  </xsl:stylesheet> 
like image 55
Mads Hansen Avatar answered Oct 24 '22 19:10

Mads Hansen