将字幕与图像相关联

时间:2015-07-31 23:04:10

标签: xml image xslt caption

我的xml中有两个图像:

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xlink="http://www.w3.org/1999/xlink">

<imageGroup>
    <image xlink:href="mums528-i001-001.png"/>
    <caption>page 1</caption>
</imageGroup>
<imageGroup>
    <image xlink:href="mums528-i001-002.png"/>
    <caption>page 2</caption>
</imageGroup>

<description>
    <title>Letter from Waldemar Schultze to Jennie Schultze</title>
    <creator type="author">
        <name type="personal">Schultze, Waldemar</name>
    </creator>
    <date>1943-06-30</date>
    <source>Special Collections and University Archives, W. E. B. Du Bois Library,
        University of Massachusetts Amherst</source>
    <citation>Robert and Waldemar Schultze Papers (MS 528). Special Collections and
        University Archives, W.E.B. Du Bois Library, University of
        Massachusetts Amherst.</citation>
</description>
<text>
        <header type="letterhead">
            <organization>Unites States Disciplinary Barracks</organization>
            <location>Fort Leavenworth, Kansas</location>
            <date format="M/DD/YY">6/30/43</date>
            <recipient>
                <name type="personal">Mrs. W.J. Schultze</name>
                <address>875 Richmond Av., Buffalo, N.Y.</address>
                <relation>Mother</relation>
            </recipient>
        </header>
    </text>
</document>

我希望我的xsl将每个标题与其相关图像放在一起,而不是将图像一起显示,然后是标题。这是我的xsl:

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

<xsl:output method="xml" indent="yes" />
<xsl:template match="/">  
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="letter.css"/>
            <title>Letter</title>
        </head>
        <body>
            <div>
                <xsl:apply-templates/>
            </div>
        </body>
    </html> 
</xsl:template>
<xsl:template match="document/description">
    <h2><xsl:value-of select="title"/></h2>
    <h3>Description</h3>
    <dl>
        <dt><span class="description">Author:&#160;</span></dt>
        <dd>
            <xsl:value-of select="creator/name"/>
        </dd>
        <dt><span class="description">Date (YYYY-MM-DD):&#160;</span></dt>
        <dd>
            <xsl:value-of select="date"/>
        </dd>
        <dt><span class="description">Source:&#160;</span></dt>
        <dd>
            <xsl:value-of select="source"/>
        </dd>
        <dt><span class="description">Pages:&#160;</span></dt>
        <dd>
            <xsl:if test="//pb">
                <xsl:value-of select="//pb[not(following::pb)]/@n"/>
            </xsl:if>
        </dd>
    </dl>
</xsl:template>
<xsl:template match="document/text/header"> 
    <h3>Text</h3>
    <xsl:if test="//imageGroup">
        <xsl:for-each select="//imageGroup">
            <p><xsl:value-of select="caption"/></p>
            <img src="{image/@xlink:href}"/>
        </xsl:for-each>
    </xsl:if>
    <span class="text"><p><span class="official"><xsl:value-of select="organization"/></span></p>
    <p><span class="official"><xsl:value-of select="location"/></span></p>
    <p><xsl:value-of select="date"/></p>
    <p><span class="official">to whom:&#160;</span><xsl:value-of select="recipient/name"/></p>
    <p><span class="official">address:&#160;</span><xsl:value-of select="recipient/address"/></p>
    <p><span class="official">relation:&#160;</span><xsl:value-of select="recipient/relation"/></p></span>
</xsl:template>
<xsl:template match="document/text/body">
    <span class="text"><p><xsl:value-of select="salutation"/></p>
    <xsl:call-template name="lines"/>
    <p><xsl:value-of select="valediction"/></p></span>
</xsl:template>
<xsl:template name="lines">
        <xsl:for-each select="//p">
            <p><xsl:value-of select="."/></p>
        </xsl:for-each>
        <xsl:for-each select="//line">
            <p><xsl:value-of select="."/></p>
        </xsl:for-each>
</xsl:template>

如何将图像和标题放在相同的xsl for-each语句中?

1 个答案:

答案 0 :(得分:0)

如果没有任何上下文(XML和XSLT),很难回答您的问题。也许这对你有用:

<xsl:for-each select="//imageGroup">
    <img src="{image/@xlink:href}"/>
    <p><xsl:value-of select="caption"/></p>
</xsl:for-each>