将标题与图像相关联

Associate captions with images

我的 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 语句中?

很难在没有任何上下文的情况下回答您的问题,XML 和 XSLT。也许这对你有用:

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