Xml XSLT:基于子节点的排序';s属性不为';行不通
我就是不能让排序函数工作 基本上,我也在尝试做同样的事情,但就是不起作用 输入是Xml XSLT:基于子节点的排序';s属性不为';行不通,xml,xslt,Xml,Xslt,我就是不能让排序函数工作 基本上,我也在尝试做同样的事情,但就是不起作用 输入是 <?xml version="1.0" encoding="UTF-8"?> <response> <result> <doc> <str name="hash1">EBFF15C2FB15BDD9C069EDF272EF43E738B276AA</str> <str name="org_data"> <
<?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>
EBFF15C2FB15BDD9C069EDF272EF43E738B276AA
8CB2237D0679CA88DB6464EAC60DA96345513964
我正在尝试这样的事情:
<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(hashnumber)和hashValue(用于调试的hashValue)属性的hashData元素
3) 清理“str”的东西
4) 尝试按hashData/@hashName对一个角色内的数据进行排序
最后一个不起作用(如果我用另一个XSL tho来做,它就起作用)。
因此,问题是-如何在相同的XSL中完成所有操作,以及为什么它不能按我的方式工作?似乎您希望对要创建的结果进行排序,因此请尝试将创建
hashData
元素的分组包装到XSL:perform sort
:
<xsl:perform-sort>
<xsl:sort select="@hashName"/>
<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>
</xsl:perform-sort>
或分组到变量中,然后通过应用模板推送变量的内容。只有在输入树或临时树中创建了具有该名称的元素时,才能应用带有
match=“roleName”
的模板。似乎要对要创建的结果进行排序,因此请尝试将创建hashData
元素的分组包装到xsl:perform sort
中:
<xsl:perform-sort>
<xsl:sort select="@hashName"/>
<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>
</xsl:perform-sort>
或分组到变量中,然后通过应用模板推送变量的内容。只有在输入树或临时树中创建了该名称的元素时,才能应用带有
match=“roleName”
的模板。一种方法是定义单个参数,由逗号分隔的哈希列表组成,然后使用tokenize
和index 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>
一种方法是定义单个参数,由逗号分隔的哈希列表组成,然后使用
标记化
和索引计算出哈希名称
这样做的好处是删除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>
谢谢!很完美!谢谢很完美!