根据ID属性将嵌套标签转换为html

时间:2013-04-30 17:16:29

标签: xml xslt

以下是问题的示例XML:

<functions>
  <function id="678">
            <name>getFunctions()</name>
            <File ID="1">
                <ncss>0</ncss>
                <ccn>0</ccn>
                <javadocs>0</javadocs>
            </File>
            <File ID="2">
                <ncss>5</ncss>
                <ccn>3</ccn>
                <javadocs>1</javadocs>
            </File>

           <function>    
        </functions>

我需要输出格式如下(表格)

FunctionId  Name            FileID    NCSS  CCN   Javadocs
678         getFunctions()  1         0      0      0
                            2         5      3      1

这个问题是当我用其他文本替换2 File标签时(例如sourceFile和destFile而不是File id =“1”和File id =“2”),我能够获得所需的输出。

但我需要它采用上述格式,我不知道如何去做。 所有输入都将受到赞赏。

谢谢。

这是我在此之前使用的XSLT代码段:

<center><h1>Function Summary</h1></center>

<table width="1000" border="1" cellspacing="5">

 <tr>
    <th width="73" align="center" valign="top">ID</th>
    <th width="176" align="center" valign="top">Name></th>
    <th colspan="2" align="center" valign="top">File</th>
    <th colspan="2" align="center" valign="top">NCSS</th>
    <th colspan="2" align="center" valign="top">CCN</th>
    <th colspan="2" align="center" valign="top">Javadocs</th>
  </tr>


  <xsl:for-each select="//function">

  <tr>
    <td align="center" valign="top">  <xsl:value-of select="@id" />  </td>
    <td align="left" valign="middle">  <xsl:value-of select="name" /> </td>

    <td width="40" align="center" valign="top">srcFile</td>

    <td width="50" align="center" valign="top">summaryFile</td>

    <td width="63" align="center" valign="top"> <xsl:value-of select="sourceFile/ncss" /> </td>
    <td width="80" align="center" valign="top"><xsl:value-of select="summaryFile/ncss" /></td>
    <td width="53" align="center" valign="top"><xsl:value-of select="sourceFile/ccn" /></td>
    <td width="66" align="center" valign="top"><xsl:value-of select="summaryFile/ccn" /></td>
    <td width="108" align="center" valign="top"><xsl:value-of select="sourceFile/javadocs" /></td>
    <td width="109" align="center" valign="top"><xsl:value-of select="summaryFile/javadocs" /></td>
  </tr>
</xsl:for-each>
</table>

sourceFile和summaryFile分别替换为File id =“1”和File id =“2”。 输出只是一个HTML表。

2 个答案:

答案 0 :(得分:1)

这会以文本格式输出

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:output method="text" indent="yes" encoding="utf-8" />
    <xsl:template match="/">
        <xsl:for-each-group select="functions/function"
            group-by="@id">

            <xsl:variable name="functionId" select="current-grouping-key()" />
            <xsl:variable name="functionName" select="name" />
            <xsl:for-each select="File">
                <xsl:choose>
                    <xsl:when test="position()=1">
                        6 fields ...
                    </xsl:when>
                    <xsl:otherwise>
                        4 fields
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:1)

据我所知,你喜欢为属于某个函数的每个File生成一个表,其中只有第一个文件有functionName和@id。

许多改变是让你的for-each遍历文件,但仅限于当前函数。如果当前文件是第一个(position()= 1)并且添加名称和@id,则进行测试。

尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns="http://www.w3.org/1999/xhtml">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="functions">
    <center>
        <h1>Function Summary</h1>
    </center>

    <table width="1000" border="1" cellspacing="5">

        <tr>
            <th width="73" align="center" valign="top">ID</th>
            <th width="176" align="center" valign="top">Name</th>
            <th align="center" valign="top">File</th>
            <th  align="center" valign="top">NCSS</th>
            <th  align="center" valign="top">CCN</th>
            <th  align="center" valign="top">Javadocs</th>
        </tr>

        <xsl:apply-templates select="function"/>

    </table>
    </xsl:template>
    <xsl:template match="function">

        <xsl:for-each select="//File">
            <tr>
                <td align="center" valign="top">
                    <xsl:if test="position()=1" >
                        <xsl:value-of select="../@id" />
                    </xsl:if>
                </td>
                <td align="left" valign="middle">
                    <xsl:if test="position()=1" >
                        <xsl:value-of select="../name" />
                    </xsl:if>
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="@ID" />
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="ncss" />
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="ccn" />
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="javadocs" />
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

将生成此输出:

<?xml version="1.0"?>
<center xmlns="http://www.w3.org/1999/xhtml">
  <h1>Function Summary</h1>
</center><table xmlns="http://www.w3.org/1999/xhtml" width="1000" border="1" cellspacing="5">
  <tr>
    <th width="73" align="center" valign="top">ID</th>
    <th width="176" align="center" valign="top">Name</th>
    <th align="center" valign="top">File</th>
    <th align="center" valign="top">NCSS</th>
    <th align="center" valign="top">CCN</th>
    <th align="center" valign="top">Javadocs</th>
  </tr>
  <tr>
    <td align="center" valign="top">678</td>
    <td align="left" valign="middle">getFunctions()</td>
    <td align="center" valign="top">1</td>
    <td align="center" valign="top">0</td>
    <td align="center" valign="top">0</td>
    <td align="center" valign="top">0</td>
  </tr>
  <tr>
    <td align="center" valign="top"/>
    <td align="left" valign="middle"/>
    <td align="center" valign="top">2</td>
    <td align="center" valign="top">5</td>
    <td align="center" valign="top">3</td>
    <td align="center" valign="top">1</td>
  </tr>
</table>

更新: 要在表格中显示空单元格,有一些可能的解决方案:

添加&nbsp;个空单元格。

<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>

使用css显示空单元格

<style type="text/css">
    table { empty-cells: show; }
</style>

更新使用rowspan连接第一个拖链列中的行。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns="http://www.w3.org/1999/xhtml">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="functions">
    <center>
        <h1>Function Summary</h1>
    </center>
        <style type="text/css">
            table { empty-cells: show; }
        </style>
    <table  border="1" cellspacing="5">

        <tr>
            <th width="73" align="center" valign="top">ID</th>
            <th width="176" align="center" valign="top">Name</th>
            <th align="center" valign="top">File</th>
            <th  align="center" valign="top">NCSS</th>
            <th  align="center" valign="top">CCN</th>
            <th  align="center" valign="top">Javadocs</th>
        </tr>

        <xsl:apply-templates select="function"/>

    </table>
    </xsl:template>
    <xsl:template match="function">
        <xsl:variable name="file_cnt" select="count(File)" />

        <xsl:for-each select="File">
            <tr>
                <xsl:if test="position()=1" >
                    <td align="center" valign="top" rowspan="{$file_cnt}">
                        <xsl:value-of select="../@id" />
                    </td>
                    <td align="center" valign="top" rowspan="{$file_cnt}">
                        <xsl:value-of select="../name" />
                    </td>
                </xsl:if>
                <td  align="center" valign="top">
                    <xsl:value-of select="@ID" />
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="ncss" />
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="ccn" />
                </td>
                <td  align="center" valign="top">
                    <xsl:value-of select="javadocs" />
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
相关问题