1

私はフォーマットのExcelで生成されたXMLから行こうとしています:

...
    <Column255>1</Column255>
    <Column256>1</Column256>
</Row>
<Row>
    <Column1>1</Column1>
    <Column2>1</Column2>
    <Column3>1</Column3>
...

(これがずっと続く)

最終的には、256x256 グリッドを次のような形式にマップします。

<map_point>    
   <x>0</x>
   <y>0</x>
   <value>1</value>
</map_point>

<map_point>    
   <x>0</x>
   <y>1</x>
   <value>1</value>
</map_point>

どうすればいいですか?

私の大まかな計画は、for-each各行で使用してxそれを座標に書き込むことでしたが、列タグがすべて異なることに困惑したためfor-each、現在はy座標を取得するために使用できません。

4

1 に答える 1

0

テンプレートを使用して、行番号をパラメーターに渡します。このようなもの:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:strip-space elements="*" />
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <map>
      <xsl:apply-templates select="*/Row" />
    </map>
  </xsl:template>

  <xsl:template match="Row">
    <xsl:apply-templates select="*[starts-with(local-name(), 'Column')]">
      <!-- position() will be 1 for the first row, 2 for the second ... -->
      <xsl:with-param name="rowNumber" select="position() - 1" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="*">
    <xsl:param name="rowNumber" />
    <map_point>
      <x><xsl:value-of select="$rowNumber" /></x>
      <!-- for the column number, strip off the leading "Column" from the
           element name, convert the rest to a number and subtract 1 -->
      <y><xsl:value-of select="substring(local-name(), 7) - 1" /></y>
      <value><xsl:value-of select="." /></value>
    </map_point>
  </xsl:template>

</xsl:stylesheet>
于 2013-06-27T11:49:10.260 に答える