如何使用模板或循环来最小化xsl中的代码

时间:2012-10-01 13:10:19

标签: xml xslt xslt-1.0

我正在尝试将预测数据从XML文件提取到我的XSLT 1.0。 我是这些语言的新手,所以我多次使用相同的代码行。给定代码的区别仅在于每个循环的@aac属性。

<xsl:for-each select="product/forecast/area[@aac='SA_PT001']">
    <xsl:value-of select="@description" /> :
    <xsl:value-of select="forecast-period/text[@type='precis']"/> : Min
    <xsl:value-of select="forecast-period/element[@type='air_temperature_minimum']"/> : Max
    <xsl:value-of select="forecast-period/element[@type='air_temperature_maximum']"/><br/>
</xsl:for-each>

<xsl:for-each select="product/forecast/area[@aac='QLD_PT015']">
    <xsl:value-of select="@description" /> :
    <xsl:value-of select="forecast-period/text[@type='precis']"/> : Min
    <xsl:value-of select="forecast-period/element[@type='air_temperature_minimum']"/> : Max
    <xsl:value-of select="forecast-period/element[@type='air_temperature_maximum']"/><br/>
</xsl:for-each>

<xsl:for-each select="product/forecast/area[@aac='NSW_PT027']">
    <xsl:value-of select="@description" /> :
    <xsl:value-of select="forecast-period/text[@type='precis']"/> : Min
    <xsl:value-of select="forecast-period/element[@type='air_temperature_minimum']"/> : Max
    <xsl:value-of select="forecast-period/element[@type='air_temperature_maximum']"/><br/>
</xsl:for-each>

你可以给我一个使用循环或XSL模板来减少代码大小的想法吗?

2 个答案:

答案 0 :(得分:1)

您可以将for-each的正文更改为模板:

<xsl:template match="area">
    <xsl:value-of select="@description" /> :
    <xsl:value-of select="forecast-period/text[@type='precis']"/> : Min
    <xsl:value-of select="forecast-period/element[@type='air_temperature_minimum']"/> : Max
    <xsl:value-of select="forecast-period/element[@type='air_temperature_maximum']"/><br/>
</xsl:template>

<xsl:template match="/">
    <xsl:apply-templates select="product/forecast/area[@aac='SA_PT001']"/>
    <xsl:apply-templates select="product/forecast/area[@aac='QLD_PT015']"/>
    <xsl:apply-templates select="product/forecast/area[@aac='NSW_PT027']"/>
</xsl:template>

这将首先为您提供所有SA_PT001区域,然后是所有QLD_PT015区域等。您可以使用

将三个选项合并为一个
<xsl:template match="/">
    <xsl:apply-templates select="product/forecast/area[@aac='SA_PT001' or @aac='QLD_PT015' or @aac='NSW_PT027']"/>
</xsl:template>

但是这会按文档顺序为您提供三个aac值中的任何一个的所有区域,而不是按aac分组。

编辑:您说您想按描述按字母顺序排序:

<xsl:template match="/">
    <xsl:apply-templates select="product/forecast/area[@aac='SA_PT001' or @aac='QLD_PT015' or @aac='NSW_PT027']">
        <xsl:sort select="@description" />
    </xsl:apply-templates>
</xsl:template>

将按描述对它们进行排序。您可以使用多个<xsl:sort>

<xsl:template match="/">
    <xsl:apply-templates select="product/forecast/area[@aac='SA_PT001' or @aac='QLD_PT015' or @aac='NSW_PT027']">
        <xsl:sort select="@aac" />
        <xsl:sort select="@description" />
    </xsl:apply-templates>
</xsl:template>

首先按aac进行分组,然后在每个aac组内按说明进行排序。

答案 1 :(得分:0)

<xsl:variable name="aac_list">NSW_PT027,QLD_PT015,NSW_PT027</xsl:variable>

<xsl:for-each select="product/forecast/area">
    <xsl:sort select="@description"/>
    <xsl:if test="contains($aac_list, ./@aac)">
        <xsl:value-of select="@description" /> :
        <xsl:value-of select="forecast-period/text[@type='precis']"/> : Min
        <xsl:value-of select="forecast-period/element[@type='air_temperature_minimum']"/> : Max
        <xsl:value-of select="forecast-period/element[@type='air_temperature_maximum']"/><br/>
    </xsl:if>
</xsl:for-each>
您可以动态构建的

aac_list变量

相关问题