简体   繁体   English

XSLT-连续显示子元素

[英]XSLT - display child elements in a row

I have the following XSLT code that displays movie information from a local XML file (title, actors, runtime, etc.) and Amazon API product information (product title and picture) from external amazon xml. 我有以下XSLT代码,它们显示来自本地XML文件的电影信息(标题,演员,运行时等),以及来自外部Amazon xml的Amazon API产品信息(产品标题和图片)。

<xsl:variable name="moviesXML" select="document('movies.xml')"/>
<xsl:variable name="inputRoot" select="/"/>

<xsl:param name="movieID"/>

<xsl:template match="/">
    <html>
        <head>
            <title>Movie details</title>
        </head>
        <body>
            <xsl:for-each select="$moviesXML/movies/movie[@movieID=$movieID]">
                <xsl:value-of select="title" />
                <xsl:value-of select="actors" />
                ...
                <xsl:apply-templates select="$inputRoot/aws:ItemLookupResponse/aws:Items/aws:Item/aws:ItemAttributes/aws:Title"/>
                <xsl:apply-templates select="$inputRoot/aws:ItemLookupResponse/aws:Items/aws:Item/aws:MediumImage/aws:URL"/>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

<xsl:template match="aws:Title">
    <xsl:value-of select="." />
    <br/>
</xsl:template>

<xsl:template match="aws:URL">
    <img src="{.}"/>
    <br/>
</xsl:template>

So based on the movieID that was passed from the previous page, the code above displays all the relevant information to that specific movie. 因此,基于从上一页传递的movieID,以上代码显示了与该特定电影有关的所有信息。 I use Amazon API to display two products for each movie (DVD and BluRay product). 我使用Amazon API为每部电影显示两种产品(DVD和BluRay产品)。

The problem I have is that my XSLT displays both Amazon product titles at once and then displays both pictures at once. 我的问题是XSLT一次显示两个Amazon产品标题,然后一次显示两个图片。 But what I want is to display Amazon product title + picture (DVD), and then another Amazon product title + picture (BluRay). 但是我要显示的是Amazon产品标题+图片(DVD),然后显示另一个Amazon产品标题+图片(BluRay)。

This is the output that I get: 这是我得到的输出:

坏

And this is what I want to achieve: 这就是我想要实现的目标:

好

You're getting what you're asking for. 您正在得到想要的东西。 These lines 这些线

<xsl:apply-templates select="$inputRoot/aws:ItemLookupResponse/aws:Items/aws:Item/aws:ItemAttributes/aws:Title"/>
<xsl:apply-templates select="$inputRoot/aws:ItemLookupResponse/aws:Items/aws:Item/aws:MediumImage/aws:URL"/>

will first apply one batch of templates, then the other. 首先将应用一批模板,然后再应用另一批模板。

You need to put the title and image together in a single template, like this: 您需要将标题和图像放在一个模板中,如下所示:

<xsl:template match="aws:Item">
    <xsl:value-of select="aws:ItemAttributes/aws:Title" />
    <br/>

    <img src="{aws:MediumImage/aws:URL}"/>
    <br/>
</xsl:template>

and then use it like this 然后像这样使用它

<xsl:apply-templates select="$inputRoot/aws:ItemLookupResponse/aws:Items/aws:Item"/>

Incidentally, this is my first time to see "too much" template decomposition in XSLT code here. 顺便说一句,这是我第一次在这里看到XSLT代码中的“太多”模板分解。 More often you'll see the opposite problem. 通常,您会看到相反的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM