2017-08-21 49 views
0

1)如何通過的總和增加一個循環對TD的總和的動態確定第動態結構​​和屬性的值

2)@鍵=「IP」 - 它也有必要使動態,將採取的值從所構建噸

<tr> 
    <th>Name</th> 
    <th>Type</th> 
    <th>Text</th> 
    <xsl:for-each-group select="//folder/element/property" group-by="@key"> 
    <th><xsl:value-of select="current-grouping-key()"/></th> 
    </xsl:for-each-group> 
</tr> 
<tr> 
    <td> 
    <xsl:value-of select="@name"/> 
    </td> 
    <td> 
    <xsl:value-of select="@*[name() = 'xsi:type'][1]"/> 
    </td> 
    <td> 
    <xsl:value-of select="documentation"/> 
    </td> 
    <xsl:for-each select="..."> 
    <td> 
     <xsl:value-of select="string-join(property[@key='ip']/@value, ', ')"/> 
    </td> 
    </xsl:for-each> 
</tr> 

輸入XML文件

<folder name="Technology &amp; Physical" type="technology"> 
    <element xsi:type="archimate:Node" name="SMX_U_TEST"> 
     <documentation>SMX</documentation> 
     <property key="ip" value="10.255.2.111"/> 
    </element> 
    <element xsi:type="archimate:Node" name="CBS3CVR"> 
     <documentation>DSR3CVR</documentation> 
     <property key="ip" value="10.15.114.24"/> 
     <property key="port" value="1521"/> 
     <property key="hw"/> 
    </element> 
    <element xsi:type="archimate:Node" name="SMX"> 
     <property key="ip" value="10.255.2.111"/> 
     <property key="port" value="8181"/> 
     <property key="port" value="8182"/> 
     <property key="port" value="8184"/> 
    </element> 
    <element xsi:type="archimate:Node" name="Informatica test"> 
     <documentation>Informatica</documentation> 
     <property key="ip" value="10.11.30.89"/> 
     <property key="port" value="1521"/> 
    </element> 
    <element xsi:type="archimate:Node" name="DSR3TEST"> 
     <documentation>DSR3TEST</documentation> 
     <property key="ip" value="10.255.3.133"/> 
     <property key="port" value="1521"/> 
     <property key="hw"/> 
    </element> 
</folder> 

outout HTML文件

<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <body> 
     <h2>Technology &amp; Physical</h2> 
     <table> 
     <tr> 
      <th>Name</th> 
      <th>Type</th> 
      <th>Text</th> 
      <th>ip</th> 
      <th>port</th> 
      <th>hw</th> 
     </tr> 
     <tr> 
      <td>CBS3CVR</td> 
      <td>archimate:Node</td> 
      <td>DSR3CVR</td> 
      <td>10.15.114.24</td> 
      <td>1521</td> 
      <td/> 
     </tr> 
     <tr> 
      <td>DSR3TEST</td> 
      <td>archimate:Node</td> 
      <td>DSR3TEST</td> 
      <td>10.255.3.133</td> 
      <td>1521</td> 
      <td/> 
     </tr> 
     <tr> 
      <td>Informatica test</td> 
      <td>archimate:Node</td> 
      <td>Informatica</td> 
      <td>10.11.30.89</td> 
      <td>1521</td> 
      <td/> 
     </tr> 
     <tr> 
      <td>SMX</td> 
      <td>archimate:Node</td> 
      <td/> 
      <td>10.255.2.111</td> 
      <td>8181, 8182, 8184</td> 
      <td/> 
     </tr> 
     <tr> 
      <td>SMX_U_TEST</td> 
      <td>archimate:Node</td> 
      <td>SMX</td> 
      <td>10.255.2.111</td> 
      <td/> 
      <td/> 
     </tr> 
     </table> 
    </body> 
    </html> 

元素和屬性的數量是無限的,並且鍵的名稱可能不同。 我用group by,如果你有另一個決定,告訴我。謝謝!!!

+0

我不明白的問題,也許添加一個例子來說明自己的目標。 –

+0

我們確實需要查看您的輸入XML樣本以及您的預期輸出,以給出準確答案。謝謝。 –

回答

0

您可以使用distinct-values在這裏得到了明顯的property鍵,而不是xsl:for-each-group

<xsl:variable name="properties" select="distinct-values(element/property/@key)" /> 

(這是假設你被定位在folder元素)。

然後,在你的XML每個element,你可以列出像這樣

<xsl:variable name="current" select="." /> 
<xsl:for-each select="$properties"> 
<td> 
    <xsl:value-of select="$current/property[@key=current()]/@value" separator=", " /> 
</td> 
</xsl:for-each> 

的屬性值試試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       exclude-result-prefixes="xsi"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="folder"> 
     <xsl:variable name="properties" select="distinct-values(element/property/@key)" /> 
     <table> 
      <tr> 
       <th>Name</th> 
       <th>Type</th> 
       <th>Text</th> 
       <xsl:for-each select="$properties"> 
       <th><xsl:value-of select="."/></th> 
       </xsl:for-each> 
      </tr> 
      <xsl:for-each select="element"> 
      <tr> 
       <td> 
       <xsl:value-of select="@name"/> 
       </td> 
       <td> 
       <xsl:value-of select="@xsi:type"/> 
       </td> 
       <td> 
       <xsl:value-of select="documentation"/> 
       </td> 
       <xsl:variable name="current" select="." /> 
       <xsl:for-each select="$properties"> 
       <td> 
        <xsl:value-of select="$current/property[@key=current()]/@value" separator=", " /> 
       </td> 
       </xsl:for-each> 
      </tr> 
      </xsl:for-each> 
     </table> 
    </xsl:template> 
</xsl:stylesheet> 

我假定你的實際XML並聲明xsi命名空間前綴。我還在XSLT中聲明檢索xsl:type屬性更簡單。

編輯:如果您只能使用XSLT 1.0處理器,您將無法使用distinct-values獲取密鑰。相反,您需要使用名爲Muenchian Grouping的技術來獲取不同的密鑰。 (您也無法使用xsl:value-of上的separator屬性)。

試試這個XSLT來代替:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       exclude-result-prefixes="xsi"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:key name="properties" match="property" use="@key" /> 

    <xsl:template match="folder"> 
     <xsl:variable name="properties" select="//property[generate-id() = generate-id(key('properties', @key)[1])]/@key" /> 
     <table> 
      <tr> 
       <th>Name</th> 
       <th>Type</th> 
       <th>Text</th> 
       <xsl:for-each select="$properties"> 
       <th><xsl:value-of select="."/></th> 
       </xsl:for-each> 
      </tr> 
      <xsl:for-each select="element"> 
      <tr> 
       <td> 
       <xsl:value-of select="@name"/> 
       </td> 
       <td> 
       <xsl:value-of select="@xsi:type"/> 
       </td> 
       <td> 
       <xsl:value-of select="documentation"/> 
       </td> 
       <xsl:variable name="current" select="." /> 
       <xsl:for-each select="$properties"> 
       <td> 
        <xsl:for-each select="$current/property[@key=current()]/@value"> 
        <xsl:if test="position() > 1">,</xsl:if> 
        <xsl:value-of select="." /> 
        </xsl:for-each> 
       </td> 
       </xsl:for-each> 
      </tr> 
      </xsl:for-each> 
     </table> 
    </xsl:template> 
</xsl:stylesheet> 
+0

什麼是'xsl:template match =「@ * | node()」'for? –

+0

和'properties'不是數組 –

+0

'xsl:template match =「@ * | node()'是身份模板,但在此實例中不需要,因此我已將其刪除。 'variable不是數組,因爲XSLT沒有數組的概念,'distinct-values'函數實際上返回一個序列(參見http://www.xsltfunctions.com/xsl/fn_distinct-values.html) –