首页 > 解决方案 > 无法读取 xsl 中的值

问题描述

<results>
  <metadata>
    <column name="title" type="S" length="4000" nullable="true" />
    <column name="schbat" type="S" length="32" nullable="true" />
    <column name="pck_cnt" type="I" length="4" nullable="true" />
  </metadata>
  <data>
    <row>
      <field>Wave</field>
      <field>1209180044-</field>
      <field>4</field>
    </row>
    <row>
      <field>Wave</field>
      <field>1209180045-</field>
      <field>1</field>
    </row>
    <row>
  </data>
</results>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:key name="kString" match="field" use="."/>
   <xsl:template match="/">
      <html>
         <body style="text-align: center;">
           <table width="450" cellpadding="5" cellspacing="0" style="border: 1px solid #CCCCCC" >
           <thead>
                <tr>
                    <th colspan="5" bgcolor="#FFFFFF" style="font-family: 'Arial', sans-serif; color: #ba1e29; border-bottom: 2px solid #ffffff; border-right: 2px solid #ffffff;"><span style="font-family: 'Oswald', sans-serif; text-align: center; font-weight: 500; color: #3f3f3f; line-height: 1; margin-bottom: 1rem; text-transform: uppercase;"><strong>Results For Processing on <span style="color: #ba1e29">5-4-2020</span></strong></span></th>
                </tr>
                <xsl:variable name="set" select="moca-results/metadata/column" />
                <xsl:variable name="count" select="count($set)" />

                <tr>
                <xsl:for-each select="$set">
                    <th width="20%" style="font-size:13px;font-family: 'Arial', sans-serif;background-color: #f0f0fa; color: #ba1e29; border-bottom: 2px solid #ffffff; border-right: 2px solid #ffffff;"><xsl:value-of select="@name" /></th>
                </xsl:for-each>
                </tr>

            </thead>
            <tbody>
                <xsl:variable name="bodyset" select="results/data/row" />
                <xsl:variable name="bodycount" select="count($bodyset)" />
                <xsl:variable name="set" select="results/metadata/column" />
                <xsl:variable name="count" select="count($set)" />
                <xsl:for-each select="$bodyset">
                    <tr>
                       <xsl:for-each select="$set">
                        <td align="center" style="font-family: 'Arial', sans-serif;background-color: #f0f0fa; color: #414143; border-right: 2px solid #ffffff;">
                            <xsl:value-of select="filed" />
                        </td>
                    </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </tbody>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

*输出

标题 | 施巴特 | pck_cnt
波 | 1209180044- | 4
波 | 1209180045- | 1

根据列数,我将在行中归档(意味着如果元数据中的列数为 3,那么我将在行元素中得到 3 个归档。但我无法读取那些归档的值(每次当我在时,我总是结果得到波浪)

标签: xmlxslt

解决方案


我有一种感觉,你把这件事复杂化了。考虑以下示例:

XML

<results>
  <metadata>
    <column name="title" type="S" length="4000" nullable="true" />
    <column name="schbat" type="S" length="32" nullable="true" />
    <column name="pck_cnt" type="I" length="4" nullable="true" />
  </metadata>
  <data>
    <row>
      <field>Wave</field>
      <field>1209180044-</field>
      <field>4</field>
    </row>
    <row>
      <field>Wave</field>
      <field>1209180045-</field>
      <field>1</field>
    </row>
  </data>
</results>

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/results">
    <html>
        <body>
            <table border="1">
                <thead>
                    <tr>
                        <xsl:for-each select="metadata/column">
                            <th>
                                <xsl:value-of select="@name" />
                            </th>
                        </xsl:for-each>
                    </tr>
                </thead>
            <tbody>
                <xsl:for-each select="data/row">
                    <tr>
                       <xsl:for-each select="field">
                            <td>
                                <xsl:value-of select="." />
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </tbody>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

结果

<html>
   <body>
      <table>
         <thead>
            <tr>
               <th>title</th>
               <th>schbat</th>
               <th>pck_cnt</th>
            </tr>
         </thead>
         <tbody>
            <tr>
               <td>Wave</td>
               <td>1209180044-</td>
               <td>4</td>
            </tr>
            <tr>
               <td>Wave</td>
               <td>1209180045-</td>
               <td>1</td>
            </tr>
         </tbody>
      </table>
   </body>
</html>

渲染:

在此处输入图像描述


推荐阅读