【问题标题】:Linking separate images into separate html pages using xsl使用 xsl 将单独的图像链接到单独的 html 页面
【发布时间】:2015-03-23 09:14:49
【问题描述】:

我正忙于一个大学项目,该项目有一个带有一堆乐队的 XML。我目前让我的 XSL 输出多个 HTML 页面,这些页面充当每个乐队的“个人资料页面”(因此每个乐队都在单独的 HTML 页面上),并带有相互链接的链接。现在,我想在相应的 html 页面上添加乐队的图像。这可能吗?

这是 XML 中一位艺术家的示例:

<lineup>
  <artist id="101">
    <name>Jeremey Loops</name>
    <genres>
      <genre>Acoustic</genre>
    </genres>
    <writeup>
      Jeremy Loops aint no traditional 'band', creating music on the spot with     that there loopdaloop pedal. Various artists also join him for free jam sessions     & the music they create is all original. it'll definitely make ya dance like a hick!
    </writeup>
    <gig>
      <day>FRIDAY</day>
      <time>
        <starts>12:00</starts>
        <ends>14:00</ends>
      </time>
    </gig>
  </artist>
</lineup>

这是我用 XSL 尝试过的,但发现图像显示在所有页面上,图像两侧都写有“真”和“假”。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="yes" name="html"/>

<xsl:template match="/">
    <xsl:apply-templates select="lineup"/>
</xsl:template>

<xsl:template match="lineup">
   <xsl:apply-templates select="artist"/>
</xsl:template>

<xsl:template match="artist">

<xsl:variable name="filename" select="concat('bands/',name,'.html')" />
<xsl:value-of select="$filename" />  
<xsl:result-document href="{$filename}" format="html">
    <html>
    <head>
        <link rel="stylesheet" type="text/css" href="../sass/test.css" />
        <title>
            <!-- TITLE OF PAGE -->
            Band - <xsl:value-of select="name"/>
        </title>
    </head>
    <body>
<!-- Adding Image -->
<div class="images">
                <xsl:value-of select="name = 'Jeremey Loops'"/>
                    <img src="images/jeremey_loops.png" alt="Jeremey Loops standing with guitar" height="350px"/>
            </div>

<!-- LINKS TO BAND PAGES -->
        <div class="box links">
            <xsl:variable name="current" select="@id" />
            <xsl:for-each select="/lineup/artist" >
                <a href="{name}.html">
                    <xsl:value-of select="name"/> | 
                </a>
            </xsl:for-each>
        </div>
    </body>
    </html>
</xsl:result-document>

</xsl:template>
</xsl:stylesheet>

所以我只想在他的页面上而不是在其他艺术家的页面上显示“Jeremey Loops”图像,其余部分也是如此。我应该为每个艺术家添加一个过滤器吗?

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    您可以使用将艺术家转换为图片网址的函数。

    假设您有一个目录“images”,其中包含文件名格式遵循模式{artist_id}_{lowercase_underscored_name}.png 的图像:

    101_jeremy_loops.png
    32_phil_beats.png
    420_snoop_dogg.png
    ...
    

    然后您可以编写一个 XSLT 函数(或者如果您坚持使用 XSLT 1.0,则可以使用类似的命名模板)从给定参数创建文件路径:

    <xsl:stylesheet version="2.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:foo="http://whatever">
    
    <!-- foo:getImgUrl("64","Phil Beats") -> "64_phil_beats.png" -->
    <xsl:function name="foo:getImgUrl">
        <xsl:param name="artist_id"/>
        <xsl:param name="artist_name"/>
    
        <xsl:value-of select="$artist_id"/>
        <xsl:text>_</xsl:text>
    
        <!-- returns lowercased artist name, replaces all non-alphanumerical
             characters with underscores. -->
        <xsl:value-of select="replace(lower-case($artist_name),'[^a-z0-9]','_')"/>
    
        <xsl:text>.png</xsl:text>
    </xsl:function>
    
    ...
    

    像这样在你的样式表中使用它:

    ...
    <img>
        <xsl:attriute name="src">
            <xsl:value-of select="foo:getImgUrl(@id, name/text())"/>
        </xsl:attribute>
    </img>
    ...
    

    【讨论】:

    • '\P{NL}' 是什么意思?当我通过 Cygwin 运行它时出现错误。 " 正则表达式中字符 3 处的语法错误:未知字符类别 NL" @Peping
    • 啊,我的错,我实际上从未处理过 xslt 中的正则表达式。我认为 '\P{NL}' 表示除字母数字以外的所有字符。显然,我无法阅读文档,您需要更像“[^a-z0-9]”的东西。我已经编辑了答案以反映这一点。来源:w3.org/TR/xmlschema-2/#charcter-classes
    • 这似乎已经解决了。现在我只需要弄清楚将这些文件写入的位置,以便我可以将当​​前图像重命名或移动到该位置! @peping
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多