How do you add an image?

后端 未结 5 1453
死守一世寂寞
死守一世寂寞 2021-02-07 06:46

Situation:

I have a simple XML document that contains image information. I need to transform it into HTML. However, I can\'t see where

相关标签:
5条回答
  • 2021-02-07 07:11

    Shouldn't that be:

    <xsl:value-of select="/root/Image/img/@src"/>
    

    ? It looks like you are trying to copy the entire Image/img node to the attribute @src

    0 讨论(0)
  • 2021-02-07 07:14

    The other option to try is a straightforward

    <img width="100" height="100" src="/root/Image/image.jpeg" class="CalloutRightPhoto"/>
    

    i.e. without {} but instead giving the direct image path

    0 讨论(0)
  • 2021-02-07 07:16

    Never mind -- I'm an idiot. I just needed <xsl:value-of select="/root/Image/node()"/>

    0 讨论(0)
  • 2021-02-07 07:22

    In order to add attributes, XSL wants

    <xsl:element name="img">
         (attributes)
    </xsl:element>
    

    instead of just

    <img>
         (attributes)
    </img>
    

    Although, yes, if you're just copying the element as-is, you don't need any of that.

    0 讨论(0)
  • 2021-02-07 07:31

    Just to clarify the problem here - the error is in the following bit of code:

    <xsl:attribute name="src">
        <xsl:copy-of select="/root/Image/node()"/>
    </xsl:attribute>
    

    The instruction xsl:copy-of takes a node or node-set and makes a copy of it - outputting a node or node-set. However an attribute cannot contain a node, only a textual value, so xsl:value-of would be a possible solution (as this returns the textual value of a node or nodeset).

    A MUCH shorter solution (and perhaps more elegant) would be the following:

    <img width="100" height="100" src="{/root/Image/node()}" class="CalloutRightPhoto"/>
    

    The use of the {} in the attribute is called an Attribute Value Template, and can contain any XPATH expression.

    Note, the same XPath can be used here as you have used in the xsl_copy-of as it knows to take the textual value when used in a Attribute Value Template.

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