簡體   English   中英

將XSLT代碼從2.0版轉換為1.0版

[英]convert XSLT code from version 2.0 to 1.0

有用於版本2.0的xslt代碼,其輸出如下所示,但我想將其轉換為版本1.0。 我引用了以下鏈接。 如何使用XSLT比較兩個XML節點並獲得比較結果? xslt比較兩個不同的節點,然后合並

輸入XML文件:

<?xml version="1.0" encoding="utf-8"?>
<OperatorStationCollection xmlns="http://www.w3.org" >
<OperatorStation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Name>OS001</Name>
    <Nodes>
      <DataNodeBase xsi:type="Adaptor">
        <Family>NetworkSettings</Family>
        <Name>Network A</Name>  
        <IPAddress>111.11.11.1</IPAddress>        
      </DataNodeBase>
      <DataNodeBase xsi:type="Adaptor">
        <Family>Network1111</Family>
        <Name>Network B</Name>                
        <IPAddress>111.22.11.1</IPAddress>          
      </DataNodeBase>
      <DataNodeBase xsi:type="Adaptor">
        <Family>Network2222</Family>
        <Name>Network C</Name>
        <IPAddress>111.33.11.1</IPAddress>
      </DataNodeBase>
      </Nodes>   
  </OperatorStation>      
<OperatorStation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Name>OS002</Name>
  <Nodes>
    <DataNodeBase xsi:type="Adaptor">
      <Family>NetworkSettings</Family>
      <Name>Network A</Name>
      <IPAddress>111.11.11.1</IPAddress>
    </DataNodeBase>
    <DataNodeBase xsi:type="Adaptor">
      <Family>Network1111</Family>
      <Name>Network B</Name>
      <IPAddress>111.22.11.2</IPAddress>
    </DataNodeBase>
    <DataNodeBase xsi:type="Adaptor">
      <Family>NetworkSettings</Family>
      <Name>Network D</Name>
      <IPAddress>111.33.11.2</IPAddress>
    </DataNodeBase>
  </Nodes>
</OperatorStation>
  <OperatorStation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Name>OS003</Name>
    <Nodes>
      <DataNodeBase xsi:type="Adaptor">
        <Family>NetworkSettings</Family>
        <Name>Network A</Name>
        <IPAddress>111.11.11.1</IPAddress>
      </DataNodeBase>
      <DataNodeBase xsi:type="Adaptor">
        <Family>NetworkSettings</Family>
        <Name>Network B</Name>
        <IPAddress>111.22.11.3</IPAddress>
      </DataNodeBase>
      <DataNodeBase xsi:type="Adaptor">
        <Family>NetworkSettings</Family>
        <Name>Network E</Name>
        <IPAddress>111.33.11.3</IPAddress>
      </DataNodeBase>
    </Nodes>
  </OperatorStation>
</OperatorStationCollection>

版本2.0的XSLT代碼:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:template match="/">
        <xsl:variable name="allStations"
                      select="/*:OperatorStationCollection/*:OperatorStation" />
        <table>
          <!-- Header row - two fixed columns plus one per station name -->
          <tr>
            <td>Name</td><td>Status</td>
            <xsl:for-each select="$allStations">
              <td><xsl:value-of select="*:Name" /></td>
            </xsl:for-each>
          </tr>
          <!-- main rows - one per "group" of DataNodeBase elements which share the
               same Name -->
          <xsl:for-each-group
              select="$allStations/*:Nodes/*:DataNodeBase"
              group-by="*:Name">
            <!-- calculate the column values - the IPAddress if this network (i.e. the
                 current-group) has an entry for this station, and "None" if not -->
            <xsl:variable name="addresses"
                select="for $s in ($allStations)
                        return (current-group()[../.. is $s]/*:IPAddress, 'None')[1]" />
            <tr>
              <td><xsl:value-of select="current-grouping-key()" /></td>
              <td>
                <!-- equal if all the $addresses are the same, unequal otherwise -->
                <xsl:value-of select="if (count(distinct-values($addresses)) = 1)
                                      then 'Equal' else 'Unequal'" />
              </td>
              <xsl:for-each select="$addresses">
                <td><xsl:value-of select="."/></td>
              </xsl:for-each>
            </tr>
          </xsl:for-each-group>
        </table>
      </xsl:template>
    </xsl:stylesheet> 

預期的輸出:由於沒有提供添加表的准備,因此我為結果制作了hatml代碼,請將此代碼保存到html文件中,然后查看預期的輸出。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>    
</head>
<body>

   <table>
            <tr>
            <td>Name</td><td>Status</td><td>OS01</td><td>OS02</td><td>OS03</td>
            </tr>
            <tr>
            <td>Network A</td><td>Equal</td><td>111.11.11.1</td><td>111.11.11.1</td><td>111.11.11.1</td>
            </tr>
            <tr>
            <td>Network B</td><td>Unequal</td><td>111.22.11.1</td><td>111.22.11.2</td><td>111.22.11.2</td>
            </tr>
            <tr>
            <td>Network C</td><td>Unequal</td><td>111.33.11.1</td><td>Not Exist</td><td>Not Exist</td>
            </tr>
            <tr>
            <td>Network D</td><td>Unequal</td><td>Not Exist</td><td>111.33.11.2</td><td>Not Exist</td>
            </tr>
            <tr>
            <td>Network E</td><td>Unequal</td><td>Not Exist</td><td>Not Exist</td><td>111.33.11.3</td>
            </tr>           
            </table>

</body>
</html>

在前面的問題中提到了Muenchian分組,因此您必須已經意識到這是采取的方法。 在這種情況下,您要按網絡名稱分組,這將構成表行的基礎,因此您需要定義一個鍵

<xsl:key name="networks" match="w3:DataNodeBase" use="w3:Name"/>

(這里使用w3:前綴是因為存在名稱空間。XML中的所有節點都在名稱空間“ http://www.w3.org ”中,而在XSLT 1.0中,您需要明確聲明這一點)

另外,您以后需要檢查每個網絡的IP地址的不同值,因此也有必要定義一個密鑰來幫助完成此操作

<xsl:key name="networksAndIP" match="w3:DataNodeBase" use="concat(w3:Name, '|', w3:IPAddress)"/>

注意| 這里的符號。 它可以是您選擇的任何字符,只要它不在NameIPAddress中出現就可以)

為了獲取作為OperatorStation元素的列,代碼與以前非常相似(主要區別在於名稱空間前綴)

<xsl:variable name="allStations" select="//w3:OperatorStation"/>

然后輸出列也和以前一樣

        <xsl:for-each select="$allStations">
           <td>
              <xsl:value-of select="w3:Name"/>
           </td>
        </xsl:for-each>

但是,第一個主要區別是,當您想為列獲取不同的網絡時。 在這里使用Muenchian分組

<xsl:apply-templates 
     select="//w3:DataNodeBase
              [generate-id() = generate-id(key('networks', w3:Name)[1])]" />

在與此匹配的模板中,您可以使用第二個鍵檢查所有IP地址是否相同

<xsl:choose>
    <xsl:when test="count(key('networksAndIP', concat(w3:Name, '|', w3:IPAddress))) = $allStationsCount">Equal</xsl:when>
    <xsl:otherwise>Unequal</xsl:otherwise>
</xsl:choose>

因此,輸出該行的IP地址只是在工作站上循環,然后將其與匹配網絡一起輸出的情況

<xsl:variable name="network" select="w3:Name"/>
<xsl:for-each select="$allStations">
   <td>
      <xsl:value-of select="key('networks', $network)[../../w3:Name=current()/w3:Name]/w3:IPAddress"/>
   </td>
</xsl:for-each>

請注意,在這種情況下,以下表達式也適用

<xsl:value-of select="w3:Nodes/w3:DataNodeBase[w3:Name=$network]/w3:IPAddress"/>

無論如何,請嘗試此XSLT

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

   <xsl:key name="networks" match="w3:DataNodeBase" use="w3:Name"/>
   <xsl:key name="networksAndIP" match="w3:DataNodeBase" use="concat(w3:Name, '|', w3:IPAddress)"/>

   <xsl:variable name="allStations" select="//w3:OperatorStation"/>
   <xsl:variable name="allStationsCount" select="count($allStations)"/>

   <xsl:template match="/">
      <table><!-- Header row - two fixed columns plus one per station name -->
         <tr>
            <td>Name</td>
            <td>Status</td>
            <xsl:for-each select="$allStations">
               <td>
                  <xsl:value-of select="w3:Name"/>
               </td>
            </xsl:for-each>
         </tr>
         <xsl:apply-templates select="//w3:DataNodeBase[generate-id() = generate-id(key('networks', w3:Name)[1])]"/>
      </table>
   </xsl:template>

   <xsl:template match="w3:DataNodeBase">
      <tr>
         <td>
            <xsl:value-of select="w3:Name"/>
         </td>
         <td>
            <xsl:choose>
               <xsl:when test="count(key('networksAndIP', concat(w3:Name, '|', w3:IPAddress))) = $allStationsCount">Equal</xsl:when>
               <xsl:otherwise>Unequal</xsl:otherwise>
            </xsl:choose>
         </td>
         <xsl:variable name="network" select="w3:Name"/>
         <xsl:for-each select="$allStations">
            <td>
               <xsl:value-of select="key('networks', $network)[../../w3:Name=current()/w3:Name]/w3:IPAddress"/>
            </td>
         </xsl:for-each>
      </tr>
   </xsl:template>
</xsl:stylesheet>

編輯:如果您希望將條目限制為某個家庭,在這種情況下,只需添加條件以檢查兩個鍵的家庭

<xsl:key name="networks" match="w3:DataNodeBase[w3:Family='NetworkSettings']" use="w3:Name"/>
<xsl:key name="networksAndIP" match="w3:DataNodeBase[w3:Family='NetworkSettings']" use="concat(w3:Name, '|', w3:IPAddress)"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM