XSLT 1.0对于每个if输出

时间:2018-11-06 02:34:29

标签: xslt xslt-1.0

我知道xslt 1.0已经完成了,但是,我花了很多时间来研究我的问题的答案。我已经看到以下问题:

XSLT for-each loop table output XSLT 1.0 Group By

我正尽力自学,但时间离我越来越远,我需要努力工作。

无论如何,我实现了我认为是解决方案的方法,并且意识到我的某些XML在每个XML中都有重复的名称,因此输出将移至下一列。我需要Visio位于Visio列(或表标题)中,而Project必须位于Project列中,等等。

这是我的XML示例

    <?xml version="1.0" standalone="yes"?>
<Root>
<v_Add_Remove_Programs>
        <ResourceID>12345678</ResourceID>
        <GroupID>5</GroupID>
        <ProdID0>Office14.PRJSTD</ProdID0>
        <DisplayName0>Microsoft Project Standard 2010</DisplayName0>
        <InstallDate0></InstallDate0>
        <Publisher0>Microsoft Corporation</Publisher0>
        <Version0>14.0.7015.1000</Version0>
    </v_Add_Remove_Programs>
    <v_Add_Remove_Programs>
        <ResourceID>12345678</ResourceID>
        <GroupID>152</GroupID>
        <ProdID0>{90140000-003A-0000-0000-0000000FF1CE}</ProdID0>
        <DisplayName0>Microsoft Office Project Standard 2010</DisplayName0>
        <InstallDate0>20180219</InstallDate0>
        <Publisher0>Microsoft Corporation</Publisher0>
        <Version0>14.0.7015.1000</Version0>
    </v_Add_Remove_Programs>
    <v_Add_Remove_Programs>
        <ResourceID>12345678</ResourceID>
        <GroupID>330</GroupID>
        <ProdID0>Office15.VISSTD</ProdID0>
        <DisplayName0>Microsoft Visio Standard 2013</DisplayName0>
        <InstallDate0></InstallDate0>
        <Publisher0>Microsoft Corporation</Publisher0>
        <Version0>15.0.4569.1506</Version0>
    </v_Add_Remove_Programs>
    <v_Add_Remove_Programs>
        <ResourceID>12345678</ResourceID>
        <GroupID>331</GroupID>
        <ProdID0>{90150000-0053-0000-0000-0000000FF1CE}</ProdID0>
        <DisplayName0>Microsoft Visio Standard 2013</DisplayName0>
        <InstallDate0>20171225</InstallDate0>
        <Publisher0>Microsoft Corporation</Publisher0>
        <Version0>15.0.4569.1506</Version0>
    </v_Add_Remove_Programs>
    <v_Add_Remove_Programs>
        <ResourceID>12345678</ResourceID>
        <GroupID>4</GroupID>
        <ProdID0>Office14.PRJSTD</ProdID0>
        <DisplayName0>Microsoft Project Standard 2010</DisplayName0>
        <InstallDate0></InstallDate0>
        <Publisher0>Microsoft Corporation</Publisher0>
        <Version0>14.0.7015.1000</Version0>
    </v_Add_Remove_Programs>
</Root>

这是我的XSLT

    <?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <html>
         <body>
            <h2>Add Remove Software</h2>
            <table border="1">
               <tr bgcolor="#9acd32">
                  <th>Visio</th>
                  <th>Project</th>
               </tr>
               <xsl:for-each select="//v_Add_Remove_Programs">
                  <xsl:variable name="DispName" select="DisplayName0" />
                  <xsl:if test="contains($DispName, 'Visio Standard') or 
            contains($DispName, 'Visio Professional')">
                     <td>
                        <xsl:value-of select="$DispName"/>
                     </td>
                  </xsl:if>
                  <xsl:if test="contains($DispName, 'Project Standard')">
                     <td>
                        <xsl:value-of select="$DispName"/>
                     </td>
                  </xsl:if>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

这是我想要的输出。

资源ID | Visio |项目
12345678 | Microsoft Visio Standard 2013 | Microsoft Project Standard 2013

非常感谢,真的很感谢。

2 个答案:

答案 0 :(得分:2)

我猜你想做类似的事情:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/Root">
    <html>
        <body>
            <h2>Add Remove Software</h2>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th>Visio</th>
                    <th>Project</th>
                </tr>
                <xsl:for-each select="v_Add_Remove_Programs">
                    <tr>
                        <td>
                            <xsl:value-of select="DisplayName0[contains(., 'Visio Standard') or 
            contains(., 'Visio Professional')]"/>
                        </td>
                        <td>
                            <xsl:value-of select="DisplayName0[contains(., 'Project Standard')]"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

给出示例输入,(渲染的)输出将是:

enter image description here

答案 1 :(得分:0)

您似乎想按ResourceID分组。在这种情况下,请阅读Muenchian Grouping。在这种情况下,您可以这样定义密钥:

<xsl:key name="resources" match="v_Add_Remove_Programs" use="ResourceID" />

要获取代表您的行的独特ResourceID元素,请执行此操作...

<xsl:for-each select="v_Add_Remove_Programs[generate-id() = generate-id(key('resources', ResourceID)[1])]">

但是您增加了一个复杂性,因为看起来您还需要在每个资源中获取不同的程序。因此,您需要使用此键进行第二次分组...

<xsl:key name="resourceNames" match="v_Add_Remove_Programs" use="concat(ResourceID, '|', DisplayName0)" />

要获取特定类型的独特程序,请执行此操作。

<xsl:for-each select="key('resources', ResourceID)
    [generate-id() = generate-id(key('resourceNames', concat(ResourceID, '|', DisplayName0))[1])] 
    [starts-with(DisplayName0, $type)]">

尝试使用此XSLT,您可以在http://xsltfiddle.liberty-development.net/6qVRKxa上看到它的运行情况

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:key name="resources" match="v_Add_Remove_Programs" use="ResourceID" />
  <xsl:key name="resourceNames" match="v_Add_Remove_Programs" use="concat(ResourceID, '|', DisplayName0)" />

  <xsl:template match="Root">
    <table>
      <tr>
        <th>Resource ID</th>
        <th>Visio</th>
        <th>Project</th>
      </tr>
      <xsl:for-each select="v_Add_Remove_Programs[generate-id() = generate-id(key('resources', ResourceID)[1])]">
        <tr>
          <td>
            <xsl:value-of select="ResourceID" />
          </td>
          <td>
            <xsl:call-template name="resourceList">
              <xsl:with-param name="type" select="'Microsoft Visio'" />
            </xsl:call-template>
          </td>
          <td>
            <xsl:call-template name="resourceList">
              <xsl:with-param name="type" select="'Microsoft Project'" />
            </xsl:call-template>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template name="resourceList">
    <xsl:param name="type" />
    <xsl:for-each select="key('resources', ResourceID)
        [generate-id() = generate-id(key('resourceNames', concat(ResourceID, '|', DisplayName0))[1])] 
        [starts-with(DisplayName0, $type)]">
      <xsl:value-of select="DisplayName0" />
      <br />
    </xsl:for-each>    
  </xsl:template>
</xsl:stylesheet>