【问题标题】:XSLT to process multiple same node in xml into one rowXSLT 将 xml 中的多个相同节点处理成一行
【发布时间】:2017-05-12 06:48:33
【问题描述】:

我是 Xslt 的新手,我想处理具有两个相同节点但只有一个不同属性的 XML。基于不同的属性输出应该在同一行上有两个不同的列。 如果数据集 ID 以 8 位数字开头,则应进入“数据集 ID 1”列;如果以 10 位数字开头,则应进入“数据集 ID 2”列 这是 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

【问题讨论】:

  • 请发布您的尝试以便我们修复它,而不必从头开始为您编写代码。
  • 我尝试了以下代码,但它给出了两行:
  • 请不要尝试在 cmets 中发布代码 - 而是编辑您的问题。

标签: xml xslt xsl-fo


【解决方案1】:

如果数据集 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('0123456789', substring(., 9, 1)))]"/>
            </td>
            <!-- 10 digits -->
            <td>
                <xsl:value-of select="$datasetIDs[contains('0123456789', 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>

渲染为:

【讨论】:

  • 嗨迈克尔,这是一个很好的解决方案,但正如你所说,同一个节点应该是连续的并且成对的,但问题是有时只有一个节点存在(另一个数据集 ID 为空,然后它不会出现进入xml)
  • 那么你需要先将它们分组。在此处了解如何操作:jenitennison.com/xslt/grouping/muenchian.html(假设您使用的是 XSLT 1.0)
  • 实际上它们仅被分组,即它们是连续的,问题是 XML 中缺少 Dataset id2 节点本身。只有 Dataset Id1 存在。(即总行数是奇数)
  • 它们是否连续并不重要。
猜你喜欢
  • 2019-04-11
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 2012-08-07
  • 2015-03-20
  • 2016-11-08
  • 1970-01-01
相关资源
最近更新 更多