首页 > 解决方案 > XSLT:基于子节点属性的排序不起作用

问题描述

我只是无法让排序功能工作。

基本上我正在尝试与XSLT 按属性值排序一样,但它只是不起作用

输入是

<?xml version="1.0" encoding="UTF-8"?>
<response>

<result>
  <doc>
    <str name="hash1">EBFF15C2FB15BDD9C069EDF272EF43E738B276AA</str>
    <str name="org_data">
<items>
<orgdata amount ="5433" />
</items>
</str>
</doc>
  <doc>
    <str name="hash1">8CB2237D0679CA88DB6464EAC60DA96345513964</str>
    <str name="org_data">
<items>
<orgdata amount_rur="300"/>
<orgdata amount_rur="100"/>
<orgdata amount_rur="200"/>
<orgdata amount_rur="200" />
</items>
</str>
 </doc>
</result>
</response>

我正在尝试这样的事情:

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

    <xsl:param name="rta-hashParticipantData1" select = "'8CB2237D0679CA88DB6464EAC60DA96345513964'"  />
    <xsl:param name="rta-hashParticipantData2"  select = "'EBFF15C2FB15BDD9C069EDF272EF43E738B276AA'" />
    <xsl:param name="rta-hashParticipantData3" />
    <xsl:param name="rta-role" select = "'123'"  />

    <xsl:template match="result">
        <xsl:copy>
            <roleName Role="{$rta-role}">
            <xsl:for-each-group select="doc" group-by="str[@name='hash1']">
                    <xsl:choose>
                        <xsl:when test="current-group()/str[@name='hash1']=$rta-hashParticipantData1">
                            <hashData hashName= '1' hashValue="{current-group()/str[@name='hash1']}" >
                                <xsl:apply-templates select="current-group()/str[@name='org_data']" />
                            </hashData>
                        </xsl:when>
                        <xsl:when test="current-group()/str[@name='hash1']=$rta-hashParticipantData2">
                            <hashData hashName= '2' hashValue="{current-group()/str[@name='hash1']}" >
                                <xsl:apply-templates select="current-group()/str[@name='org_data']" />
                            </hashData>
                        </xsl:when>
                        <xsl:when test="current-group()/str[@name='hash1']=$rta-hashParticipantData3">
                            <hashData hashName= '3' hashValue="{current-group()/str[@name='hash1']}" >
                                <xsl:apply-templates select="current-group()/str[@name='org_data']" />
                            </hashData>
                        </xsl:when>
                    </xsl:choose>
            </xsl:for-each-group>
            </roleName>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="photoUrls|str">
        <xsl:apply-templates select="*"/>
    </xsl:template>
   <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
 <xsl:template match="roleName">
    <xsl:copy>
      <xsl:apply-templates select="hashData">
        <xsl:sort select="@hashName"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

所以我:

1) 按 hash1 值对数据进行分组

2)基于全局参数添加一些信息(角色和哈希数)。更具体地说,我添加了带有 hashName(哈希数)和 hashValue(用于调试的哈希值)属性的 hashData 元素。

3)清洁“str”的东西

4) 尝试通过 hashData/@hashName 对一个角色内的数据进行排序

最后一个不起作用(如果我用另一个 XSL 做它,它会起作用)。所以,问题是 - 如何在同一个 XSL 中完成这一切,为什么它不能按我的方式工作?

标签: xmlxslt

解决方案


一种方法是定义一个参数,由逗号分隔的哈希列表组成,然后使用tokenizeindex-of计算出 hashName。

这将具有删除xsl:choose, 并允许超过 3 个参数的优点。

试试这个 XSLT

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

    <xsl:param name="rta-role" select = "'123'"  />

    <xsl:param name="rta-hashParticipantData" select="'8CB2237D0679CA88DB6464EAC60DA96345513964,EBFF15C2FB15BDD9C069EDF272EF43E738B276AA'" />

    <xsl:template match="result">
        <xsl:variable name="rta-hashParticipantDataList" select="tokenize($rta-hashParticipantData, ',')" />
        <xsl:copy>
            <roleName Role="{$rta-role}">
                <xsl:for-each-group select="doc" group-by="str[@name='hash1']">
                    <xsl:sort select="index-of($rta-hashParticipantDataList, current-grouping-key())" />
                    <hashData hashName="{index-of($rta-hashParticipantDataList, current-grouping-key())}" hashValue="{current-group()/str[@name='hash1']}" >
                        <xsl:apply-templates select="current-group()/str[@name='org_data']" />
                    </hashData>
                </xsl:for-each-group>
            </roleName>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="photoUrls|str">
        <xsl:apply-templates select="*"/>
    </xsl:template>

   <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

推荐阅读