如果父项没有匹配的子项,则为XSLT选择节点

时间:2012-06-01 00:10:17

标签: xslt xpath unique tablerow

我有xml的源代码:

        <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="2" class="OddLegacy">
            <TableCell>
                <Paragraph>10.140.50.50</Paragraph></TableCell>
            <TableCell>
                <Paragraph>Sun Solaris 8</Paragraph></TableCell>
            <TableCell>
                <Paragraph>53.1</Paragraph></TableCell>
            <TableCell>
                <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                    <ListItem>opusone.dal.veritas.com</ListItem>
                    <ListItem>OPUSONE</ListItem></UnorderedList></TableCell></TableRow>
        <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="3" class="Even">
            <TableCell>
                <Paragraph>10.140.50.163</Paragraph></TableCell>
            <TableCell>
                <Paragraph>Sun Solaris 8</Paragraph></TableCell>
            <TableCell>
                <Paragraph>53.0</Paragraph></TableCell>
            <TableCell>
                <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                    <ListItem>dns1.dal.veritas.com</ListItem>
                    <ListItem>opusone.dal.veritas.com</ListItem></UnorderedList></TableCell></TableRow>
        <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="4" class="OddLegacy">
            <TableCell>
                <Paragraph>10.140.50.82</Paragraph></TableCell>
            <TableCell>
                <Paragraph>HP-UX 870849686</Paragraph></TableCell>
            <TableCell>
                <Paragraph>31.4</Paragraph></TableCell>
            <TableCell>
                <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                    <ListItem>unknown</ListItem>
                    <ListItem>testarossa.dal.veritas.com</ListItem></UnorderedList></TableCell></TableRow>
        <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="5" class="Even">
            <TableCell>
                <Paragraph>10.140.50.71</Paragraph></TableCell>
            <TableCell>
                <Paragraph>Microsoft Windows Server 2008 R2, Enterprise Edition</Paragraph></TableCell>
            <TableCell>
                <Paragraph>30.5</Paragraph></TableCell>
            <TableCell>
                <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                    <ListItem>EVSERV1.evexample.local</ListItem>
                    <ListItem>EVSERV1</ListItem></UnorderedList></TableCell></TableRow>

如果操作系统(Microsoft Windows Server 2008 R2,企业版)是唯一的,我需要为源中的每一行输出一个表行。 到目前为止,我有:

                <xsl:for-each select="Table/TableRow[TableCell[2]/Paragraph[not(preceding-sibling::TableCell[2]/Paragraph)]]">

                    <xsl:sort select="TableCell[2]/Paragraph"/>
                        <xsl:variable name="newosname" select="TableCell[2]/Paragraph"/>
                        <xsl:variable name="oscount" select="count(parent::Table/TableRow[TableCell/Paragraph = $newosname])"/>
                        <tr>
                            <td>
                                <xsl:value-of select="$newosname"/>
                                <xsl:value-of select ="TableCell[2]/Paragraph[not(preceding::TableCell[2]/Paragraph)]"/>
                            </td>
                            <td>
                                <xsl:value-of select="$oscount"/>
                            </td>
                        </tr>
                </xsl:for-each>

注意前面的测试不仅仅是我尝试测试轴'。

预期输出

<tr>
<td>Sun Solaris 8</td>
<td>2</td>
</tr>
<tr>
<td>HP-UX 870849686</td>
<td>1</td>
</tr>
....

2 个答案:

答案 0 :(得分:0)

以下模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="oss" as="xs:string+">
            <xsl:sequence select="distinct-values( Table/TableRow/TableCell[2]/Paragraph/text() )"/>
        </xsl:variable>
        <xsl:variable name="table" select="Table/TableRow"/>
        <xsl:for-each select="$oss">
            <xsl:sort select="."/>
            <tr>
                <td>
                    <xsl:value-of select="."/>
                </td>
                <td>
                    <xsl:value-of select="count($table[TableCell[2]/Paragraph/text() = current()])"/>
                </td>
            </tr>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

应用于提供的输入XML,提供以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<tr>
    <td>HP-UX 870849686</td>
    <td>1</td>
</tr>
<tr>
    <td>Microsoft Windows Server 2008 R2, Enterprise Edition</td>
    <td>1</td>
</tr>
<tr>
    <td>Sun Solaris 8</td>
    <td>2</td>
</tr>

答案 1 :(得分:0)

此XSLT 1.0转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kRowByOs" match="TableRow"
  use="TableCell[2]/Paragraph"/>

 <xsl:template match=
  "TableRow
     [generate-id()
     =
      generate-id(key('kRowByOs', TableCell[2]/Paragraph)[1])
     ]">
  <tr>
   <td><xsl:value-of select="TableCell[2]/Paragraph"/></td>
   <td><xsl:value-of select=
    "count(key('kRowByOs', TableCell[2]/Paragraph))"/></td>
  </tr>
  </xsl:template>

  <xsl:template match="text()"/>
</xsl:stylesheet>

应用于以下XML文档(提供的片段包装在单个顶部元素中):

<t>
            <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="2" class="OddLegacy">
                <TableCell>
                    <Paragraph>10.140.50.50</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>Sun Solaris 8</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>53.1</Paragraph></TableCell>
                <TableCell>
                    <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                        <ListItem>opusone.dal.veritas.com</ListItem>
                        <ListItem>OPUSONE</ListItem></UnorderedList></TableCell></TableRow>
            <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="3" class="Even">
                <TableCell>
                    <Paragraph>10.140.50.163</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>Sun Solaris 8</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>53.0</Paragraph></TableCell>
                <TableCell>
                    <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                        <ListItem>dns1.dal.veritas.com</ListItem>
                        <ListItem>opusone.dal.veritas.com</ListItem></UnorderedList></TableCell></TableRow>
            <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="4" class="OddLegacy">
                <TableCell>
                    <Paragraph>10.140.50.82</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>HP-UX 870849686</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>31.4</Paragraph></TableCell>
                <TableCell>
                    <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                        <ListItem>unknown</ListItem>
                        <ListItem>testarossa.dal.veritas.com</ListItem></UnorderedList></TableCell></TableRow>
            <TableRow TableRowLevel="1" RowTitle="" TableRowNumber="5" class="Even">
                <TableCell>
                    <Paragraph>10.140.50.71</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>Microsoft Windows Server 2008 R2, Enterprise Edition</Paragraph></TableCell>
                <TableCell>
                    <Paragraph>30.5</Paragraph></TableCell>
                <TableCell>
                    <UnorderedList UnorderedListLevel="1" class="compactList" UnorderedListNumber="1">
                        <ListItem>EVSERV1.evexample.local</ListItem>
                        <ListItem>EVSERV1</ListItem></UnorderedList></TableCell></TableRow>
</t>

会产生想要的正确结果:

<tr>
   <td>Sun Solaris 8</td>
   <td>2</td>
</tr>
<tr>
   <td>HP-UX 870849686</td>
   <td>1</td>
</tr>
<tr>
   <td>Microsoft Windows Server 2008 R2, Enterprise Edition</td>
   <td>1</td>
</tr>

解释

正确使用 Muenchian method for grouping