1

我正在尝试通过 XSLT 将 XML 数据导入 Adob​​e InDesign。我想获取接下来四天(不是今天)的信息。到目前为止,我只能获得明天的预测,但不确定如何输出四天。模板选择第 2 天(明天)和时间段 2(一天中的时间)

我可以访问这种格式的免费天气数据:http ://www.yr.no/place/Iceland/Capital_Region/Reykjavik/forecast.xml

这是模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="ISO-8859-1" indent="yes" omit-xml-declaration="no" cdata-section-elements=""/>
  <xsl:strip-space elements="*"/>

  <!--
  The position of the time[@period = '2'] element; indexing starts from 1.
  You can pass in this parameter to the transformation if you want the
  time[@period = '1'] element in some other position.
  -->
  <xsl:param name="time" select="2"/> <!-- This is the day -->
  <xsl:param name="base" select="'file:///Volumes/Media/Geymsla/ymis_verkefni/DV2013/sky/'"/>

  <xsl:template match="/">
    <yr>
      <testtag>
        <xsl:apply-templates select="weatherdata"/>
      </testtag>
    </yr>
  </xsl:template>

  <xsl:template match="weatherdata">
    <xsl:apply-templates select="location/name"/>
    <xsl:apply-templates select="links/link[@id = 'overview']"/>
    <base><xsl:value-of select="$base"/></base>
    <xsl:apply-templates select="meta/lastupdate"/>
    <!-- Apply the <time period="2"> element in $time position, the time of day -->
    <xsl:apply-templates select="forecast/tabular/time[@period = '2'][number($time)]"/>
  </xsl:template>

  <xsl:template match="tabular/time">
    <xsl:apply-templates select="symbol"/>
    <xsl:apply-templates select="precipitation"/>
    <xsl:apply-templates select="temperature"/>
    <xsl:apply-templates select="windSpeed"/>
    <xsl:apply-templates select="windDirection"/>

    <!--
    Use attribute value template (AVT) to construct the attribute value:
    http://lenzconsulting.com/how-xslt-works/#attribute_value_templates
    -->
    <Image href="{concat($base, symbol/@var, '.png')}"/>
  </xsl:template>

  <xsl:template match="location/name">
    <title>
      <xsl:value-of select="."/>
    </title>
  </xsl:template>

  <xsl:template match="links/link">
    <alternate>
      <!-- Use the value of the @url attribute of this element -->
      <xsl:value-of select="@url"/>
    </alternate>
  </xsl:template>

  <xsl:template match="temperature">
    <tempval>
      <xsl:value-of select="@value"/>
    </tempval>
  </xsl:template>

  <xsl:template match="windSpeed">
    <speedname>
      <xsl:value-of select="@name"/>
    </speedname>

    <speedmps>
      <xsl:value-of select="@mps"/>
    </speedmps>
  </xsl:template>

  <xsl:template match="windDirection">
    <dirdeg>
      <xsl:value-of select="@deg"/>
    </dirdeg>

    <dircode>
      <xsl:value-of select="@code"/>
    </dircode>

    <dirname>
      <xsl:value-of select="@name"/>
    </dirname>
  </xsl:template>

  <xsl:template match="precipitation">
    <precip>
      <xsl:value-of select="@value"/>
    </precip>
  </xsl:template>

  <xsl:template match="symbol">
    <symbolvar>
      <xsl:value-of select="@var"/>
    </symbolvar>

    <symbolnr>
      <xsl:value-of select="@number"/>
    </symbolnr>

    <symbolname>
      <xsl:value-of select="@name"/>
    </symbolname>
  </xsl:template>

  <xsl:template match="lastupdate">
    <!-- Copy the original node as is -->
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
4

1 回答 1

0

您需要调整谓词过滤器。您当前正在选择所有的time元素 who's @period=2,然后从那些匹配的元素中选择第二个:

<!-- Apply the <time period="2"> element in $time position, the time of day -->
<xsl:apply-templates select="forecast/tabular/time[@period = '2'][number($time)]"/>

为了选择您将使用的参数@period指定的参数:$time

<xsl:apply-templates select="forecast/tabular/time[@period = number($time)]"/>

然后,您可以通过在该集合上应用谓词过滤器来归档接下来的四天:

<xsl:apply-templates select="forecast/tabular/time[@period = number($time)]
                                       [position() > 1 and position() &lt;= 5]"/>
于 2013-05-22T02:55:10.730 回答