xslt how to add attributes to copy-of

后端 未结 1 932
再見小時候
再見小時候 2020-12-28 13:25

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


In the XML file the node

相关标签:
1条回答
  • 2020-12-28 14:21

    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>
    
    0 讨论(0)
提交回复
热议问题