XSLT按动态属性值选择

时间:2015-02-23 17:18:54

标签: arrays xml xslt properties

我有以下代表2D数组的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Prop Name='Test' Type='Array' LBound='[0][0]' HBound='[9][9]' ElementType='String' Flags='0x0'>
    <Value ID='[0][0]'>1</Value>
    <Value ID='[1][0]'>2</Value>
    <Value ID='[2][0]'>3</Value>
    <Value ID='[0][1]'>10</Value>
    <Value ID='[1][1]'>11</Value>
    <Value ID='[2][1]'>12</Value>       
</Prop>

'ID'属性中的第一个括号内的值是行,第二个是数组中的列。 'Prop'中'Value'元素的实际数量可能会有所不同,但我总是会有一个2D数组。

我需要将其格式化为一个包含2列的HTML表格,如下所示:

enter image description here

为此,我有以下XSLT基本上通过并打印到第一列所有以'[0]'结尾的元素,然后尝试找到以'[1]'结尾的匹配元素:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   

<xsl:template match="/">
  <html>
  <body>
  <h2>Name Value Pairs</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Value</th>  
      </tr>   
      <xsl:for-each select="Prop[@Name='Test']/Value">
        <xsl:if test="contains(self::Value/@ID,'][0]')">
          <tr>  
            <td><xsl:value-of select="self::Value"/></td>
            <td><xsl:value-of select="parent::Prop/Value[@ID=concat('[',position()-1,'][1]')]"/></td>                       
          </tr>  
        </xsl:if>
      </xsl:for-each>    
    </table>
  </body>
  </html>
</xsl:template>    
</xsl:stylesheet>

但是,这会将第二列返回为空,问题似乎是当我尝试使用@ID属性中的concat函数动态更改其值时。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

根据您的输入XML,这个XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
  <xsl:template match="/">
    <html>
      <body>
        <h2>Name Value Pairs</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th style="text-align:left">Name</th>
            <th style="text-align:left">Value</th>  
          </tr>   
          <xsl:for-each select="Prop[@Name='Test']/Value">
            <xsl:if test="contains(@ID,'][0]')">
              <xsl:variable name="pos" select="position()"/>
              <tr>  
                <td><xsl:value-of select="."/></td>
                <td>
                  <xsl:value-of select="../Value[@ID=concat('[',$pos - 1,'][1]')]"/>
                </td>                       
              </tr>  
            </xsl:if>
          </xsl:for-each>    
        </table>
      </body>
    </html>
  </xsl:template>    
</xsl:stylesheet>

将生成此HTML:

<html>
   <body>
      <h2>Name Value Pairs</h2>
      <table border="1">
         <tr bgcolor="#9acd32">
            <th style="text-align:left">Name</th>
            <th style="text-align:left">Value</th>
         </tr>
         <tr>
            <td>1</td>
            <td>10</td>
         </tr>
         <tr>
            <td>2</td>
            <td>11</td>
         </tr>
         <tr>
            <td>3</td>
            <td>12</td>
         </tr>
      </table>
   </body>
</html>

这样呈现:

enter image description here

相关问题