I tried something lik...">

How to add xsl attribute

I have xml with img tag

<img>
source
</img>

I want to generate:

<img src="source.jpg">

I tried something like this:

<img>
<xsl:attribute name="src">
  <xsl:text>
        <xsl:value-of select="node()" />.jpg
      </xsl:text>
    </xsl:attribute>
</img> 

but it works

+3
source share
4 answers

The reason you are not executing is because you cannot evaluate XSLT expressions inside an element <xsl:text>.

<xsl:text>can only contain literals, entities, and #PCDATA .

If you move <xsl:value-of>out <xsl:text>, the following will work:

    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="node()" />
            <xsl:text>.jpg</xsl:text>
        </xsl:attribute>
    </img>

However, the choice <xsl:value-of select="node()>for <img>in your example will include carriage returns and white space characters inside the element <img>, which probably doesn't match your attribute src.

normalize-space() . :

    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="normalize-space(node())" />
            <xsl:text>.jpg</xsl:text>
        </xsl:attribute>
    </img>

<xsl:text> Fabiano, :

    <img>
        <xsl:attribute name="src"><xsl:value-of select="normalize-space(node())" />.jpg</xsl:attribute>
    </img> 
+4

<img src="{normalize-space()}.jpg"/>

, <img> node.

+5

xsl: text, . :

<img>   
    <xsl:attribute name="src">
        <xsl:value-of select="concat(node(), '.jpg')"/>
    </xsl:attribute>
</img>

, . =)

+1
<img>
    <xsl:attribute name="src">
        <xsl:value-of select="my_photo/@href" />
    </xsl:attribute>
</img>

<my_photo href="folder/poster.jpg" /> 
+1

Source: https://habr.com/ru/post/1756247/


All Articles