我正在尝试将预测数据从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模板来减少代码大小的想法吗?
答案 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
变量