How to call an image correctly through XSL from a

2019-05-24 06:45发布

I have an issue with calling an image across to my transformed xsl file.

It seems to be something very simple, yet I can't find a solution to it. I have tried searching on here already and using the various similar answers provided but have had no luck.

I'm trying to use the string inside

<image>string</image>

as the filepath to display my graphic.

Any resultant I've tried always gives me the small broken graphic icon.

Thanks in advance.

EDIT: I'll be more specific. (Sorry it's been a long day)

I'm using Notepad++, with the "XML Tools" plugin, I can apply the XSL Transformation by specifying the xsl file when I am focused on the XML file. Where I save it as a newly created html file.

Opening it should display all my data + the image. But instead, I get the placeholder instead for when a graphic cannot be located. (i.e. in Internet Explorer its the small icon with the red cross.)

Using the stylesheet method call at the start of the file should also allow me to view the transformation without the need to create any new file using Firefox. But it also fails to display.

Here is my XML:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet href="aperture_event_page.xsl" type="text/xsl"?>
    <listings
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="aperture_venues.xsd">

        <venue id="apv_01">
            <image>/images/apv_01.png</image>
            <other elements></other elements>
            ...
            <more elements>
                <elements></elements>
                ...
            </more elements>
        </venue>

        <venue id="apv_02">
            #repeat#
        </venue>

        <venue id="apv_03">
            #repeat#
        </venue>

        <venue id="apv_04">
            #repeat#
        </venue>
    </listings/>

and my XSL:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:element name="html">
            <xsl:element name="head">
                <xsl:element name="title">Event</xsl:element>
            </xsl:element>

            <xsl:element name="body">
                <xsl:element name="image">
                    <img src="{image}"/>
                </xsl:element>
                <xsl:apply-templates select="/listings/venue/image"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

标签: xml xslt image
2条回答
一夜七次
2楼-- · 2019-05-24 07:20
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" omit-xml-declaration="yes"/>
    <xsl:template match="/">
    <xsl:element name="html">
    <xsl:element name="head">
    <xsl:element name="title">Event: Concert</xsl:element>
    </xsl:element>
    <xsl:element name="body">
    <xsl:apply-templates select="/listings/venue/event[@id='eve_01']"/>
    </xsl:element>
    </xsl:element>
    </xsl:template>
    <xsl:template match="event">
    <xsl:element name="img">
    <xsl:attribute name="src">
    <xsl:text>
    http://url to image path/xml/task2/event_page
    </xsl:text>
    <xsl:value-of select="../image/text()"/>
    </xsl:attribute>
    </xsl:element>
    </xsl:template>
    </xsl:stylesheet>

Using xsl:text, I was able to specify the image path url and then take in the image element string as the filename to complete the full path.

查看更多
别忘想泡老子
3楼-- · 2019-05-24 07:27

You're using <xsl:element> where you don't need it and failing to use it in the one place you do.

There's not quite enough here to tell me what you're after completely, but this should get you closer:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Event</title>
            </head>

            <body>
                <xsl:apply-templates select="/listings/venue/image"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="image">
        <xsl:element name="img">
            <xsl:attribute name="src">
                <xsl:value-of select="." />
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

 </xsl:stylesheet>

This will produce a sequence of html <img> tags with the src attribute properly containing an image path from each <image> tag in your original xml.

If you want your output to be xhtml instead of plain html (and thus have the tags properly terminated as xml), you could change the output-method to "xml" if you're using XSL 1.0 or "xhtml" if you're using XSL 2.0.

查看更多
登录 后发表回答