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 回答