2017-05-12 82 views
-1

我是Xslt的新手,我想處理具有兩個相同節點但只有一個不同屬性的XML。根據不同的屬性輸出應該在同一行有兩個不同的列。 如果數據集ID具有8位數字它應該進入「集ID 1」列&如果它始於10位數字它應該去「集ID 2」列 這裏開始是XML-XSLT將xml中的多個相同節點處理成一行

<?xml version="1.0" encoding="ISO-8859-1"?> 

    <DisplayDefinitionTable> 
     <columns> 
      <column_entry order_num="1">Name</column_entry> 
      <column_entry order_num="2">Id</column_entry> 
      <column_entry order_num="3">DatasetID</column_entry> 
     </columns> 
     <rows> 
      <row> 
       <object_tag tag="45106" uid="yfVhkbLv6Vq5bD"/> 
       <object_tag tag="45922" uid="BebdkpIm6Vq5bD"/> 
       <row_element column="1" component_tag="45925" property_name="name">DEM</row_element> 
       <row_element column="2" component_tag="45925" property_name="Id">8888431618</row_element> 
       <row_element column="3" component_tag="50853" property_name="DatasetID">31661449AA</row_element> 

      </row> 
      <row> 
       <object_tag tag="45106" uid="yfVhkbLv6Vq5bD"/> 
       <object_tag tag="45922" uid="BebdkpIm6Vq5bD"/> 
       <row_element column="1" component_tag="45925" property_name="name">DEM</row_element> 
       <row_element column="2" component_tag="45925" property_name="Id">8888431618</row_element> 
       <row_element column="3" component_tag="50854" property_name="DatasetID">8888431618A</row_element> 

      </row> 
      <row> 
       <object_tag tag="45175" uid="HReh0zDS6Vq5bD"/> 
       <object_tag tag="45922" uid="BebdkpIm6Vq5bD"/> 
       <row_element column="1" component_tag="51997" property_name="name">CEM</row_element> 
       <row_element column="2" component_tag="51997" property_name="Id">8888516207</row_element> 
       <row_element column="3" component_tag="52010" property_name="DatasetID">8888516207/C</row_element> 

      </row> 
      <row> 
       <object_tag tag="45175" uid="HReh0zDS6Vq5bD"/> 
       <object_tag tag="45922" uid="BebdkpIm6Vq5bD"/> 

       <row_element column="1" component_tag="51997" property_name="name">CEM</row_element> 
       <row_element column="2" component_tag="51997" property_name="Id">8888516207</row_element> 
       <row_element column="3" component_tag="52011" property_name="DatasetID">31661809AB</row_element> 

      </row> 
      <row> 
       <object_tag tag="44593" uid="07Uh0rzi6Vq5bD"/> 
       <object_tag tag="45922" uid="BebdkpIm6Vq5bD"/> 
       <row_element column="1" component_tag="52019" property_name="name">TT</row_element> 
       <row_element column="2" component_tag="52019" property_name="Id">8888574081</row_element> 
       <row_element column="3" component_tag="52992" property_name="DatasetID">8888574081/C</row_element> 

      </row> 
      <row> 
       <object_tag tag="44593" uid="07Uh0rzi6Vq5bD"/> 
       <object_tag tag="45922" uid="BebdkpIm6Vq5bD"/> 
       <row_element column="1" component_tag="52019" property_name="name">TT</row_element> 
       <row_element column="2" component_tag="52019" property_name="Id">8888574081</row_element> 
       <row_element column="3" component_tag="52993" property_name="DatasetID">31691071/AC</row_element> 

      </row> 
     </DisplayDefinitionTable> 

我期待像

Name ID DatasetId1 DatasetId2 
DEM 8888431618 31661449AA 8888431618A 
CEM 8888516207 31661809AB 8888516207/C 
+0

請發表您的嘗試,所以我們可以修復它,而不必寫信給你r從頭開始編寫代碼。 –

+0

我試過下面的代碼,但它給了兩行: –

+0

請不要嘗試在評論中發佈代碼 - 而是編輯你的問題。 –

回答

0

輸出如果數據集ID與8位數字就應該進入「集ID 1」 &列開始,如果它開始與10位數字就應該到 「集ID 2」列

與製劑中的問題是,如果字符串與10位數字開頭,則根據定義它也開始以8位的數字。所以,假設這些是唯一的兩種可能性,測試應該集中在第9個字符是否是數字。

以下內容還假定行按連續對排列,共有Id

XSLT 1.0

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

<xsl:template match="/DisplayDefinitionTable"> 
    <table> 
    <tr> 
     <th>Name</th> 
     <th>ID</th> 
     <th>DatasetId 1</th> 
     <th>DatasetId 2</th> 
    </tr> 
    <xsl:for-each select="rows/row[position() mod 2 = 1]"> 
     <tr> 
      <!-- Name --> 
      <td> 
       <xsl:value-of select="row_element[@property_name='name']"/> 
      </td> 
      <!-- ID --> 
      <td> 
       <xsl:value-of select="row_element[@property_name='Id']"/> 
      </td> 
      <!-- Dataset IDs --> 
      <xsl:variable name="datasetIDs" select="(. | following-sibling::row[1])/row_element[@property_name='DatasetID']" /> 
      <!-- 8 digits --> 
      <td> 
       <xsl:value-of select="$datasetIDs[not(contains('', substring(., 9, 1)))]"/> 
      </td> 
      <!-- 10 digits --> 
      <td> 
       <xsl:value-of select="$datasetIDs[contains('', substring(., 9, 1))]"/> 
      </td> 
     </tr> 
    </xsl:for-each> 
    </table> 
</xsl:template> 

</xsl:stylesheet> 

應用到你的輸入例子,結果將是:

<?xml version="1.0" encoding="UTF-8"?> 
<table> 
    <tr> 
    <th>Name</th> 
    <th>ID</th> 
    <th>DatasetId 1</th> 
    <th>DatasetId 2</th> 
    </tr> 
    <tr> 
    <td>DEM</td> 
    <td>8888431618</td> 
    <td>31661449AA</td> 
    <td>8888431618A</td> 
    </tr> 
    <tr> 
    <td>CEM</td> 
    <td>8888516207</td> 
    <td>31661809AB</td> 
    <td>8888516207/C</td> 
    </tr> 
    <tr> 
    <td>TT</td> 
    <td>8888574081</td> 
    <td>31691071/AC</td> 
    <td>8888574081/C</td> 
    </tr> 
</table> 

呈現爲:

enter image description here

+0

嗨邁克爾,這是非常好的解決方案,但正如你所說的同一個節點應該在連續和對,但問題有時只有一個節點存在(另一個數據集標識爲空,那麼它不會進入到XML) –

+0

然後,你需要先**組**。在這裏學習如何做到這一點:http://www.jenitennison.com/xslt/grouping/muenchian.html(假設你使用XSLT 1.0) –

+0

實際上它們只被分組,即它們是連續的,問題是數據集id2節點本身從XML中缺失。只有數據集Id1存在(即總行數是奇數) –

相關問題