how to add xsl attribute

后端 未结 4 671
长情又很酷
长情又很酷 2020-12-21 03:50

I have an xml with img tag


source

I want to generate:


相关标签:
4条回答
  • 2020-12-21 03:59

    Use:

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

    This assumes the <img> element is the current node.

    0 讨论(0)
  • 2020-12-21 04:02

    The reason why what you are doing does not work is that you cannot evaluate XSLT expressions inside of the <xsl:text> element.

    <xsl:text> can only contain literal text, entity references, and #PCDATA.

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

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

    However, selecting <xsl:value-of select="node()> for the <img> in your example will include the carriage returns and whitespace characters inside of the <img> element, which is probably not what you want in your src attribute value.

    That is why Dimitre Novatchev used normalize-space() in his answer. Applying that to the example above:

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

    If you get rid of the <xsl:text> as Fabiano's solution suggests, then you could also do this:

        <img>
            <xsl:attribute name="src"><xsl:value-of select="normalize-space(node())" />.jpg</xsl:attribute>
        </img> 
    
    0 讨论(0)
  • 2020-12-21 04:14
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="my_photo/@href" />
        </xsl:attribute>
    </img>
    
    <my_photo href="folder/poster.jpg" /> 
    
    0 讨论(0)
  • 2020-12-21 04:17

    Just remove the tag xsl:text, in this case, you won't need it. Try this:

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

    I didn't test it, but it should work. =)

    0 讨论(0)
提交回复
热议问题