在xslt中对缺少的表元素进行分组和填充

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在xslt中对缺少的表元素进行分组和填充相关的知识,希望对你有一定的参考价值。

我有一个像下面给出的xml。我试图将此xml转换为html表。 “ServerName”节点成为列标题。行分为“类别”名称。当任何“数据”节点不存在“类别 - >名称”或“参数 - >键”时,它应显示“NA”。

我尽力自己解决这个问题,但是在分组和填补缺失的元素时我失败了。任何帮助都非常感谢。

<Results>
  <Data>
    <ServerName>Server1</ServerName>
    <CategorizedData>
      <Category>
        <Parameters>
          <Parameter key="A" value="1" />
          <Parameter key="B" value="2" />
          <Parameter key="C" value="3" />
        </Parameters>
      </Category>
      <Category Name="Misc">
        <Parameters>
          <Parameter key="A" value="2" />
          <Parameter key="B" value="5" />
        </Parameters>
      </Category>
    </CategorizedData>
  </Data>
  <Data>
    <ServerName>Server2</ServerName>
    <CategorizedData>
      <Category>
        <Parameters>
          <Parameter key="A" value="10" />
          <Parameter key="B" value="20" />
          <Parameter key="D" value="30" />
        </Parameters>
      </Category>
      <Category Name="Misc">
        <Parameters>
          <Parameter key="C" value="2" />
          <Parameter key="D" value="5" />
        </Parameters>
      </Category>
    </CategorizedData>
  </Data>
</Results>

<table>
	<tr>
		<td></td>
		<td>Server1</td>
		<td>Server2</td>
	</tr>
	<tr>
		<td>A</td>
		<td>1</td>
		<td>10</td>
	</tr>
	<tr>
		<td>B</td>
		<td>2</td>
		<td>20</td>
	</tr>
	<tr>
		<td>C</td>
		<td>3</td>
		<td>NA</td>
	</tr>
	<tr>
		<td>D</td>
		<td>NA</td>
		<td>30</td>
	</tr>
	<tr>
		<td>Misc</td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>A</td>
		<td>2</td>
		<td>NA</td>
	</tr>
	<tr>
		<td>B</td>
		<td>5</td>
		<td>NA</td>
	</tr>
	<tr>
		<td>C</td>
		<td>NA</td>
		<td>2</td>
	</tr>
	<tr>
		<td>D</td>
		<td>NA</td>
		<td>5</td>
	</tr>
</table>
答案

使用XSLT 1:示例:https://xsltfiddle.liberty-development.net/bFukv8n

<?xml version="1.0" encoding="UTF-8"?>

<xsl:template match="/">
    <xsl:variable name="vMsg" select="."/>
    <xsl:variable name="vServerNames" select="/Results/Data/ServerName" />
    <table>
        <tr>
            <td/>
            <xsl:for-each select="$vServerNames">
                <td>
                    <xsl:value-of select="."/>
                </td>
            </xsl:for-each>
        </tr>
        <!-- Categories with no Name -->
        <xsl:for-each select="$vMsg//Category[not(@Name)]/Parameters/Parameter[not(@key = following::Category[not(@Name)]/Parameters/Parameter/@key)]">
            <xsl:variable name="vCurrKey" select="@key" />
            <tr>
                <td>
                    <xsl:value-of select="$vCurrKey"/>
                </td>
                <xsl:for-each select="$vServerNames">
                    <xsl:variable name="vCurrServer" select="text()" />
                    <xsl:variable name="vCurrVal" select="$vMsg/Results/Data[ServerName = $vCurrServer]/CategorizedData/Category[not(@Name)]/Parameters/Parameter[@key = $vCurrKey]"/>
                    <xsl:choose>
                        <xsl:when test="boolean($vCurrVal)">
                            <td>
                                <xsl:value-of select="$vCurrVal/@value"/>
                            </td>
                        </xsl:when>
                        <xsl:otherwise>
                            <td>NA</td>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
        <!-- Categories with Name -->
        <xsl:for-each select="//Category[@Name and not(@Name = following::Category/@Name)]">
            <xsl:variable name="vCatName" select="@Name"/>
            <tr>
                <td>
                    <xsl:value-of select="$vCatName"/>
                </td>
                <td/>
                <td/>
            </tr>
            <xsl:variable name="vKeys" select="$vMsg//Category[@Name = $vCatName]/Parameters/Parameter[not(@key = following::Category[@Name = $vCatName]/Parameters/Parameter/@key)]" />
            <xsl:for-each select="$vKeys">
                <xsl:variable name="vCurrKey" select="@key" />
                <tr>
                    <td>
                        <xsl:value-of select="$vCurrKey"/>
                    </td>
                    <xsl:for-each select="$vServerNames">
                        <xsl:variable name="vCurrServer" select="text()" />
                        <xsl:variable name="vCurrVal" select="$vMsg/Results/Data[ServerName = $vCurrServer]/CategorizedData/Category[@Name = $vCatName]/Parameters/Parameter[@key = $vCurrKey]"/>
                        <xsl:choose>
                            <xsl:when test="boolean($vCurrVal)">
                                <td>
                                    <xsl:value-of select="$vCurrVal/@value"/>
                                </td>
                            </xsl:when>
                            <xsl:otherwise>
                                <td>NA</td>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </xsl:for-each>
    </table>
</xsl:template>

另一答案

使用XSLT 2或3,您可以使用嵌套的for-each-group解决它:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    expand-text="yes"
    version="3.0">


  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>.NET XSLT Fiddle Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Results">
      <table>
          <xsl:variable name="servers" select="Data/ServerName"/>
          <thead>
              <tr>
                  <th>Key</th>
                  <xsl:for-each select="$servers">
                      <th>{.}</th>
                  </xsl:for-each>
              </tr>
          </thead>
          <tbody>
              <xsl:for-each-group select=".//Category" group-by="string(@Name)">
                  <xsl:variable name="cat-group" select="current-group()"/>
                  <tr>
                      <th>{current-grouping-key()}</th>
                      <th>{$servers[1]}</th>
                      <th>{$servers[2]}</th>
                  </tr>
                  <xsl:for-each-group select="current-group()" group-by="Parameters/Parameter/@key">
                      <tr>
                          <td>{current-grouping-key()}</td>
                          <td>{($cat-group[ancestor::Data[ServerName = $servers[1]]]/Parameters/Parameter[@key = current-grouping-key()]/@value, 'N/A')[1]}</td>
                          <td>{($cat-group[ancestor::Data[ServerName = $servers[2]]]/Parameters/Parameter[@key = current-grouping-key()]/@value, 'N/A')[1]}</td>
                      </tr>
                  </xsl:for-each-group>
              </xsl:for-each-group>
          </tbody>
      </table>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/6qM2e2i/3给出了输出

  <table>
     <thead>
        <tr>
           <th>Key</th>
           <th>Server1</th>
           <th>Server2</th>
        </tr>
     </thead>
     <tbody>
        <tr>
           <th></th>
           <th>Server1</th>
           <th>Server2</th>
        </tr>
        <tr>
           <td>A</td>
           <td>1</td>
           <td>10</td>
        </tr>
        <tr>
           <td>B</td>
           <td>2</td>
           <td>20</td>
        </tr>
        <tr>
           <td>C</td>
           <td>3</td>
           <td>N/A</td>
        </tr>
        <tr>
           <td>D</td>
           <td>N/A</td>
           <td>30</td>
        </tr>
        <tr>
           <th>Misc</th>
           <th>Server1</th>
           <th>Server2</th>
        </tr>
        <tr>
           <td>A</td>
           <td>2</td>
           <td>N/A</td>
        </tr>
        <tr>
           <td>B</td>
           <td>5</td>
           <td>N/A</td>
        </tr>
        <tr>
           <td>C</td>
           <td>N/A</td>
           <td&g

以上是关于在xslt中对缺少的表元素进行分组和填充的主要内容,如果未能解决你的问题,请参考以下文章

XSLT使用for-each-group进行分组并使用填充元素

在 XSLT 中对每个父节点下的子节点进行分组

在 XSLT 中对记录进行分组时如何避免 O(n^2) 复杂性?

如何在 xslt 元素上应用 group by

使用 XSLT muenchian-grouping 进行嵌套分组

使用 XSLT 程序对具有逗号分隔值的 XML 元素进行分组