2

XSLTを介してXMLデータをAdobeInDesignにインポートしようとしています。InDesignは、属性を表示できても、属性を使用できないようです。

私はこの形式の無料の気象データにアクセスできます:http ://www.yr.no/sted/Norge/Oslo/Oslo/Oslo/varsel.xml

XSLTを使用して属性を通常のXML形式に変換する方法が最適かどうかはわかりませんが、機能します。

私が抱えている問題は、明日の天気データが必要なのですが、今日のデータしか取得できないということです。データはweatherdata/Forecast / tabular/timeに保存されます。

<xsl:param name="speedmps" select="weatherdata/forecast/tabular/time[@period='2']/windSpeed/@mps" />はデータを選択するために使用しますが、これは最初の一致のみを選択します。そして、最初の試合は今日の天気(12:00から18:00)で、私が欲しいのは2番目の試合(2番目の期間=「2」の試合、つまり明日の12:00から18:00)です。日は4つの期間で表示され、XMLファイルは数日間の予報を取得しました。理想的には、今日の天気用に1つ、明日の天気用に1つのXSLTを作成したいと思います。

私のコードはほとんどカットアンドペーストされており、それから私はそれを編集して私が望むことをしようとしています。ifブロックを作成しようとしましたが、構築できる例が見つかりませんでした。XSLTでの私のスキルは、現時点では基本的すぎて、期限が短いです。そして、ソースデータで私が発見したのは、データを取得する日の時期に応じて期間が変化することです。したがって、明日の期間= '2'はこの時間帯の5番目である可能性があるため、6番目のブロックを選択する必要があるとは言えません。(period = '0'は00:00から09:00であるため、期間0は09:00以降に削除されます)。

現在の最初の試合は次のとおりです。

<time from="2013-02-27T12:00:00" to="2013-02-27T18:00:00" period="2">
    <!-- Valid from 2013-02-27T12:00:00 to 2013-02-27T18:00:00 -->
    <symbol number="1" name="Klarvær" var="01d" />
    <precipitation value="0" />
    <!-- Valid at 2013-02-27T12:00:00 -->
    <windDirection deg="187.2" code="S" name="Sør" />
    <windSpeed mps="1.5" name="Flau vind" />
    <temperature unit="celsius" value="-1" />
    <pressure unit="hPa" value="1030.2" />
  </time>    

現在の2番目の試合は次のとおりです。

<time from="2013-02-28T12:00:00" to="2013-02-28T18:00:00" period="2">
    <!-- Valid from 2013-02-28T12:00:00 to 2013-02-28T18:00:00 -->
    <symbol number="1" name="Klarvær" var="01d" />
    <precipitation value="0" />
    <!-- Valid at 2013-02-28T12:00:00 -->
    <windDirection deg="181.3" code="S" name="Sør" />
    <windSpeed mps="3.1" name="Svak vind" />
    <temperature unit="celsius" value="2" />
    <pressure unit="hPa" value="1015.7" />
  </time>

申し訳ありませんが、英語は私の母国語ではありません。できるだけ上手に説明しようとしました。

これは私のXSLTコードです:

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

<!-- params -->
<xsl:param name="location" select="weatherdata/location/name"/>
<xsl:param name="alternate" select="weatherdata/links/link[@id='overview']/@url"/>
<xsl:param name="base" select="'file:///d:/temp/yr/sky/'"/>
<!-- We use the text forecast dates or? -->
<xsl:param name="lastupdate" select="weatherdata/meta/lastupdate" />
<xsl:param name="symbolnumber" select="weatherdata/forecast/tabular/time[@period='2']/symbol/@number" />
<xsl:param name="symbolname" select="weatherdata/forecast/tabular/time[@period='2']/symbol/@name" />
<xsl:param name="symbolvar" select="weatherdata/forecast/tabular/time[@period='2']/symbol/@var" />
<xsl:param name="precip" select="weatherdata/forecast/tabular/time[@period='2']/precipitation/@value" />
<xsl:param name="directiondeg" select="weatherdata/forecast/tabular/time[@period='2']/windDirection/@deg" />
<xsl:param name="directioncode" select="weatherdata/forecast/tabular/time[@period='2']/windDirection/@code" />
<xsl:param name="directionname" select="weatherdata/forecast/tabular/time[@period='2']/windDirection/@name" />
<xsl:param name="speedmps" select="weatherdata/forecast/tabular/time[@period='2']/windSpeed/@mps" />
<xsl:param name="speedname" select="weatherdata/forecast/tabular/time[@period='2']/windSpeed/@name" />
<xsl:param name="tempvalue" select="weatherdata/forecast/tabular/time[@period='2']/temperature/@value" />

<xsl:template match="/">
<yr>
<testtag>   
    <title><xsl:value-of select="$location"/></title>
    <alternate><xsl:value-of select="$alternate"/></alternate>
    <base><xsl:value-of select="$base"/></base>
    <lastupdate><xsl:value-of select="$lastupdate"/></lastupdate>
    <symbolvar><xsl:value-of select="$symbolvar"/></symbolvar>
    <symbolnr><xsl:value-of select="$symbolnumber"/></symbolnr>
    <symbolname><xsl:value-of select="$symbolname"/></symbolname>
    <precip><xsl:value-of select="$precip"/></precip>
    <tempval><xsl:value-of select="$tempvalue"/></tempval>
    <speedname><xsl:value-of select="$speedname"/></speedname>
    <speedmps><xsl:value-of select="$speedmps"/></speedmps>
    <dirdeg><xsl:value-of select="$directiondeg"/></dirdeg>
    <dircode><xsl:value-of select="$directioncode"/></dircode>
    <dirname><xsl:value-of select="$directionname"/></dirname>
    <Image><xsl:attribute name="href"><xsl:value-of select="$base"/><xsl:value-of select="$symbolvar"/>.png</xsl:attribute></Image>
</testtag>  
</yr>
</xsl:template> 

</xsl:stylesheet>
4

1 に答える 1

0

2 番目のtime[@period = '2']要素が必要な場合 (つまり、私が正しく理解している場合)、 を使用できますtime[@period='2'][2]

ただし、スタイルシートは実際には XSLT を行う方法ではありません。ノード値を<xsl:param>要素に格納して後で処理する代わりに、処理する入力ドキュメント内の各ノードに要素を含めるという慣習があり<xsl:template>ます。

目的を達成する方法はたくさんありますが、以下のスタイルシートをコードの出発点として使用できます。

入力

の属性値を<time>持つ2 番目:@period2

<time from="2013-02-28T12:00:00" to="2013-02-28T18:00:00" period="2">
    <!-- Valid from 2013-02-28T12:00:00 to 2013-02-28T18:00:00 -->
    <symbol number="1" name="Klarvær" var="01d" />
    <precipitation value="0" />
    <!-- Valid at 2013-02-28T12:00:00 -->
    <windDirection deg="181.3" code="S" name="Sør" />
    <windSpeed mps="3.1" name="Svak vind" />
    <temperature unit="celsius" value="2" />
    <pressure unit="hPa" value="1015.7" />
</time>

スタイルシート

<?xml version="1.0" encoding="ISO-8859-1"?>
<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 = '2'] element in some other position.
  -->
  <xsl:param name="time" select="2"/>
  <xsl:param name="base" select="'file:///d:/temp/yr/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 -->
    <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>

出力

<?xml version="1.0" encoding="ISO-8859-1"?>
<yr>
  <testtag>
    <title>Oslo</title>
    <alternate>http://www.yr.no/sted/Norge/Oslo/Oslo/Oslo/</alternate>
    <base>file:///d:/temp/yr/sky/</base>
    <lastupdate>2013-02-27T06:39:00</lastupdate>
    <symbolvar>01d</symbolvar>
    <symbolnr>1</symbolnr>
    <symbolname>Klarv?r</symbolname>
    <precip>0</precip>
    <tempval>2</tempval>
    <speedname>Svak vind</speedname>
    <speedmps>3.1</speedmps>
    <dirdeg>181.3</dirdeg>
    <dircode>S</dircode>
    <dirname>S?r</dirname>
    <Image href="file:///d:/temp/yr/sky/01d.png"/>
  </testtag>
</yr>
于 2013-02-27T09:36:06.100 に答える