2017-05-12 68 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存在(即总行数是奇数) –

相关问题