0

我有以下 XML

    <ArrayOfOperatorStation>
     <OperatorStation>
        <Name>OS1</Name>
        <OS>Microsoft Windows XP Professional</OS>
        <IP>172.20.254.90</IP>
        <Version>5.1.2600</Version>
        <Alive>true</Alive>
        <Files>
          <FileComparison>
            <Filename>C:\file1.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
          </FileComparison>
          <FileComparison>
            <Filename>C:\file3.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
          </FileComparison>
        </Files>
    </OperatorStation>
    <OperatorStation>
        <Name>OS2</Name>
        <OS>Microsoft Windows XP Professional</OS>
        <IP>172.20.254.91</IP>
        <Version>5.1.2600</Version>
        <Alive>true</Alive>
        <Files>
          <FileComparison>
            <Filename>C:\file1.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
          </FileComparison>
          <FileComparison>
            <Filename>C:\file2.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
          </FileComparison>
        </Files>
  </OperatorStation>
</ArrayOfOperatorStation>

并希望在最终的 html 结果中生成此数据的不同表。

Operator staion
----------------
OS1     |  OS2
Alive   |  Alive

and another one with a filelist
OS1    | OS2    | etc...
---------------------------
File1  | File1
File3  | File2

我在制作第二个列表时遇到了麻烦,我不知道如何开始。完全用“查询”。

4

1 回答 1

1

这是您的第二张桌子的解决方案。其中还考虑了 OperatorStation 的文件数量可能不同的可能性。

尝试这样的事情:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" indent="yes"/>

    <xsl:key name="kOperatorStationFiles" match="FileComparison"  use="../preceding-sibling::OperatorStation[1]/Name" />

    <xsl:template match="OperatorStation" mode="files">
        <xsl:param name="fileNr" />
        <td>
            <xsl:value-of select="key('kOperatorStationFiles', Name)[$fileNr]/Filename"/>
        </td>
    </xsl:template>

    <xsl:template match="OperatorStation" mode="header">
        <td>
            <xsl:value-of select="Name"/>
        </td>
    </xsl:template>

    <xsl:template match="/">
        <table>
            <tr>
                <xsl:apply-templates select="//OperatorStation" mode="header"/>
            </tr>
            <!-- find max files -->
            <xsl:for-each select="//OperatorStation" >
                <xsl:sort select="count(key('kOperatorStationFiles', Name))" order="descending" />
                <xsl:if test="position()=1">
                    <xsl:for-each select="key('kOperatorStationFiles', Name)" >
                        <tr>
                        <xsl:apply-templates select="//OperatorStation" mode="files">
                            <xsl:with-param name="fileNr" select="position()" />
                        </xsl:apply-templates>
                        </tr>
                    </xsl:for-each>
                </xsl:if>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

有了这个(改变的)输入:

<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfOperatorStation>
    <OperatorStation>
        <Name>OS1</Name>
        <OS>Microsoft Windows XP Professional</OS>
        <IP>172.20.254.90</IP>
        <Version>5.1.2600</Version>
        <Alive>true</Alive>
    </OperatorStation>
    <Files>
        <FileComparison>
            <Filename>C:\file1.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
        </FileComparison>
        <FileComparison>
            <Filename>C:\file3.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
        </FileComparison>
    </Files>

    <OperatorStation>
        <Name>OS2</Name>
        <OS>Microsoft Windows XP Professional</OS>
        <IP>172.20.254.91</IP>
        <Version>5.1.2600</Version>
        <Alive>true</Alive>
    </OperatorStation>
    <Files>
        <FileComparison>
            <Filename>C:\file1.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
        </FileComparison>
        <FileComparison>
            <Filename>C:\file2.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
        </FileComparison>
        <FileComparison>
            <Filename>C:\file471.txt</Filename>
            <Hash>82c7aa19c690bb41e91c6b71d22bb533</Hash>
        </FileComparison>

    </Files>
</ArrayOfOperatorStation>

它生成以下输出:

<?xml version="1.0"?>
<table>
  <tr>
    <td>OS1</td>
    <td>OS2</td>
  </tr>
  <tr>
    <td>C:\file1.txt</td>
    <td>C:\file1.txt</td>
  </tr>
  <tr>
    <td>C:\file3.txt</td>
    <td>C:\file2.txt</td>
  </tr>
  <tr>
    <td/>
    <td>C:\file471.txt</td>
  </tr>
</table>

更新:由于注释“文件必须包含在 -parent 中。 ”一些小的更改会做:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" indent="yes"/>

    <xsl:key name="kOperatorStationFiles" match="FileComparison"  use="ancestor::OperatorStation/Name" />

    <xsl:template match="OperatorStation" mode="files">
        <xsl:param name="fileNr" />
        <td>
            <xsl:value-of select="key('kOperatorStationFiles', Name)[$fileNr]/Filename"/>
        </td>

    </xsl:template>

    <xsl:template match="OperatorStation" mode="header">
        <td>
            <xsl:value-of select="Name"/>
        </td>
    </xsl:template>

    <xsl:template match="/*">
        <table>
            <tr>
                <xsl:apply-templates select="/*/OperatorStation" mode="header"/>

            </tr>
            <!-- find max files -->
            <xsl:for-each select="/*/OperatorStation" >
                <xsl:sort select="count(key('kOperatorStationFiles', Name))" order="descending" />
                <xsl:if test="position()=1">
                    <xsl:for-each select="key('kOperatorStationFiles', Name)" >
                        <tr>
                        <xsl:apply-templates select="/*/OperatorStation" mode="files">
                            <xsl:with-param name="fileNr" select="position()" />
                        </xsl:apply-templates>
                        </tr>
                    </xsl:for-each>

                </xsl:if>
            </xsl:for-each>
        </table>
    </xsl:template>

</xsl:stylesheet>
于 2013-05-31T14:13:22.107 回答