在XSLT中对数值进行排序。

huangapple go评论62阅读模式
英文:

sort the values in XSLT

问题

I've translated the content you provided. Here's the translated text:

我正在努力对多个数组组的值进行排序。
我希望基于同一年和同一月的相同数组和多个数组来分组值。

**输入XML**

<?xml version="1.0" ?>
<message:GenericData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:generic="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic"
xmlns:message="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message"
xmlns:common="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common">

&lt;message:DataSet structureRef=&quot;BBK_SEDI&quot; &gt;
    &lt;generic:Series&gt; 
        &lt;generic:SeriesKey&gt;  
            &lt;generic:Value id=&quot;BBK_SEDI_CALCULATION&quot; value=&quot;ABZINS7&quot;&gt;&lt;/generic:Value&gt;        
        &lt;/generic:SeriesKey&gt;
        &lt;generic:Attributes&gt;
            &lt;generic:Value id=&quot;BBK_ID&quot; value=&quot;BBSDI.M.ABZINS7.R01&quot;&gt;&lt;/generic:Value&gt;
        &lt;/generic:Attributes&gt;
        &lt;generic:Obs&gt;
            &lt;generic:ObsDimension value=&quot;2015-01&quot;&gt;&lt;/generic:ObsDimension&gt;
            &lt;generic:ObsValue value=&quot;3.07&quot;&gt;&lt;/generic:ObsValue&gt;
        &lt;/generic:Obs&gt;
        &lt;generic:Obs&gt;
            &lt;generic:ObsDimension value=&quot;2016-01&quot;&gt;&lt;/generic:ObsDimension&gt;
            &lt;generic:ObsValue value=&quot;3.05&quot;&gt;&lt;/generic:ObsValue&gt; 
        &lt;/generic:Obs&gt;
    &lt;/generic:Series&gt;
    &lt;generic:Series&gt;
        &lt;generic:SeriesKey&gt;       
            &lt;generic:Value id=&quot;BBK_SEDI_CALCULATION&quot; value=&quot;ABZINS7&quot;&gt;&lt;/generic:Value&gt;						
        &lt;/generic:SeriesKey&gt;
        &lt;generic:Attributes&gt;
            &lt;generic:Value id=&quot;BBK_ID&quot; value=&quot;BBSDI.M.ABZINS7.R02&quot;&gt;&lt;/generic:Value&gt;
        &lt;/generic:Attributes&gt;
        &lt;generic:Obs&gt;
            &lt;generic:ObsDimension value=&quot;2015-01&quot;&gt;&lt;/generic:ObsDimension&gt;
            &lt;generic:ObsValue value=&quot;1.07&quot;&gt;&lt;/generic:ObsValue&gt;
        &lt;/generic:Obs&gt;
        &lt;generic:Obs&gt;
            &lt;generic:ObsDimension value=&quot;2016-01&quot;&gt;&lt;/generic:ObsDimension&gt;
            &lt;generic:ObsValue value=&quot;1.05&quot;&gt;&lt;/generic:ObsValue&gt;
        &lt;/generic:Obs&gt; 
    &lt;/generic:Series&gt;
&lt;/message:DataSet&gt;

</message:GenericData>


我的XSLT仅从相同的数组中提供值,而不是来自不同数组的值。

**XSLT**

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xslt/java" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:n0="http://sap.com/xi/SAPGlobal20/Global"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:generic="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic"
xmlns:message="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message"
xmlns:common="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common"
xmlns:ns1="urn:siemens.com:Rates:SIE_I_T_S4_FIN1.0"
exclude-result-prefixes="xs xsi map generic java n0 message common ns1 ext exsl" xmlns:xslt-version="MonthlyDiscounts_XSLT_26/06/2022" xmlns:exsl="http://exslt.org/common" xmlns:ext="http://exslt.org/common">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="message:DataSet">
<ns1:DiscountInterestRatePercent>
<xsl:for-each select="./generic:Series " >
<xsl:if test= " ./generic:SeriesKey/generic:Value/@value = 'ABZINS7'">

		&lt;Data&gt;
            &lt;Year&gt;
			&lt;xsl:value-of select=&quot;substring-before(./generic:Obs/generic:ObsDimension/@value,&#39;-&#39;)&quot;/&gt;
			&lt;/Year&gt;
            &lt;Month&gt;
			    &lt;xsl:value-of select=&quot;substring-after(./generic:Obs/generic:ObsDimension/@value,&#39;-&#39;)&quot;/&gt;
			&lt;/Month&gt;
			&lt;RatePercentList&gt;
			&lt;xsl:for-each select=&quot;./generic:Obs&quot; &gt;
                &lt;RatePercent&gt;
				   &lt;xsl:value-of select=&quot;./generic:ObsValue/@value&quot;/&gt;
				&lt;/RatePercent&gt;
			&lt;/xsl:for-each&gt;
			&lt;/RatePercentList&gt;
		&lt;/Data&gt;
	&lt;/xsl:if&gt;
	&lt;/xsl:for-each&gt;
英文:

I am struggling to get sorting the values from multiple arrays groups.
I want group the values based upon same year and same month respective of same array and multiple arrays.

Input XML

&lt;?xml version=&quot;1.0&quot; ?&gt;
&lt;message:GenericData xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:generic=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic&quot;
 xmlns:message=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message&quot;
 xmlns:common=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common&quot;&gt;

    &lt;message:DataSet structureRef=&quot;BBK_SEDI&quot; &gt;
        &lt;generic:Series&gt; 
            &lt;generic:SeriesKey&gt;  
                &lt;generic:Value id=&quot;BBK_SEDI_CALCULATION&quot; value=&quot;ABZINS7&quot;&gt;&lt;/generic:Value&gt;        
            &lt;/generic:SeriesKey&gt;
            &lt;generic:Attributes&gt;
                &lt;generic:Value id=&quot;BBK_ID&quot; value=&quot;BBSDI.M.ABZINS7.R01&quot;&gt;&lt;/generic:Value&gt;
            &lt;/generic:Attributes&gt;
            &lt;generic:Obs&gt;
                &lt;generic:ObsDimension value=&quot;2015-01&quot;&gt;&lt;/generic:ObsDimension&gt;
                &lt;generic:ObsValue value=&quot;3.07&quot;&gt;&lt;/generic:ObsValue&gt;
            &lt;/generic:Obs&gt;
            &lt;generic:Obs&gt;
                &lt;generic:ObsDimension value=&quot;2016-01&quot;&gt;&lt;/generic:ObsDimension&gt;
                &lt;generic:ObsValue value=&quot;3.05&quot;&gt;&lt;/generic:ObsValue&gt; 
            &lt;/generic:Obs&gt;
		&lt;/generic:Series&gt;
		&lt;generic:Series&gt;
            &lt;generic:SeriesKey&gt;       
                &lt;generic:Value id=&quot;BBK_SEDI_CALCULATION&quot; value=&quot;ABZINS7&quot;&gt;&lt;/generic:Value&gt;						
            &lt;/generic:SeriesKey&gt;
            &lt;generic:Attributes&gt;
                &lt;generic:Value id=&quot;BBK_ID&quot; value=&quot;BBSDI.M.ABZINS7.R02&quot;&gt;&lt;/generic:Value&gt;
            &lt;/generic:Attributes&gt;
            &lt;generic:Obs&gt;
                &lt;generic:ObsDimension value=&quot;2015-01&quot;&gt;&lt;/generic:ObsDimension&gt;
                &lt;generic:ObsValue value=&quot;1.07&quot;&gt;&lt;/generic:ObsValue&gt;
            &lt;/generic:Obs&gt;
            &lt;generic:Obs&gt;
                &lt;generic:ObsDimension value=&quot;2016-01&quot;&gt;&lt;/generic:ObsDimension&gt;
                &lt;generic:ObsValue value=&quot;1.05&quot;&gt;&lt;/generic:ObsValue&gt;
            &lt;/generic:Obs&gt; 
		&lt;/generic:Series&gt;
    &lt;/message:DataSet&gt;
&lt;/message:GenericData&gt;

My XSLT provides gives the values from same arrays not from different arrays

**XSLT**

    &lt;?xml version=&quot;1.0&quot; ?&gt;
    &lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; 
    xmlns:java=&quot;http://xml.apache.org/xslt/java&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; 
    xmlns:map=&quot;http://www.w3.org/2005/xpath-functions/map&quot; xmlns:n0=&quot;http://sap.com/xi/SAPGlobal20/Global&quot; 
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xmlns:generic=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic&quot;
     xmlns:message=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message&quot;
     xmlns:common=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common&quot;
     xmlns:ns1=&quot;urn:siemens.com:Rates:SIE_I_T_S4_FIN1.0&quot;
    exclude-result-prefixes=&quot;xs xsi map generic java n0 message common ns1 ext exsl&quot; xmlns:xslt-version=&quot;MonthlyDiscounts_XSLT_26/06/2022&quot; xmlns:exsl=&quot;http://exslt.org/common&quot; xmlns:ext=&quot;http://exslt.org/common&quot;&gt;
    	&lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot; omit-xml-declaration=&quot;yes&quot;/&gt;
    	&lt;xsl:strip-space elements=&quot;*&quot;/&gt;
    	&lt;xsl:template match=&quot;message:DataSet&quot;&gt;
    		&lt;ns1:DiscountInterestRatePercent&gt;
    		&lt;xsl:for-each select=&quot;./generic:Series &quot; &gt;
    		&lt;xsl:if test= &quot; ./generic:SeriesKey/generic:Value/@value = &#39;ABZINS7&#39;&quot;&gt;
    		
    			&lt;Data&gt;
                    &lt;Year&gt;
    				&lt;xsl:value-of select=&quot;substring-before(./generic:Obs/generic:ObsDimension/@value,&#39;-&#39;)&quot;/&gt;
    				&lt;/Year&gt;
                    &lt;Month&gt;
    				    &lt;xsl:value-of select=&quot;substring-after(./generic:Obs/generic:ObsDimension/@value,&#39;-&#39;)&quot;/&gt;
    				&lt;/Month&gt;
    				&lt;RatePercentList&gt;
    				&lt;xsl:for-each select=&quot;./generic:Obs&quot; &gt;
                        &lt;RatePercent&gt;
    					   &lt;xsl:value-of select=&quot;./generic:ObsValue/@value&quot;/&gt;
    					&lt;/RatePercent&gt;
    				&lt;/xsl:for-each&gt;
    				&lt;/RatePercentList&gt;
    			&lt;/Data&gt;
    		&lt;/xsl:if&gt;
    		&lt;/xsl:for-each&gt;
    			
    		&lt;/ns1:DiscountInterestRatePercent&gt;
    	&lt;/xsl:template&gt;
    	&lt;/xsl:stylesheet&gt;

Expected output

I am expecting an output based upon below format

&lt;ns1:DiscountInterestRatePercent xmlns:xslt-version=&quot;MonthlyDiscounts_XSLT_26/06/2022&quot;
                                                                xmlns:ns1=&quot;urn:ates:SI&quot;&gt;
   &lt;Data&gt;
      &lt;Year&gt;2015&lt;/Year&gt;
      &lt;Month&gt;01&lt;/Month&gt;
      &lt;RatePercentList&gt;
         &lt;RatePercent&gt;3.07&lt;/RatePercent&gt;
         &lt;RatePercent&gt;1.07&lt;/RatePercent&gt;
      &lt;/RatePercentList&gt;
   &lt;/Data&gt;
   &lt;Data&gt;
      &lt;Year&gt;2016&lt;/Year&gt;
      &lt;Month&gt;01&lt;/Month&gt;
      &lt;RatePercentList&gt;
         &lt;RatePercent&gt;3.05&lt;/RatePercent&gt;
         &lt;RatePercent&gt;1.05&lt;/RatePercent&gt;
        
      &lt;/RatePercentList&gt;
   &lt;/Data&gt;
&lt;/ns1:DiscountInterestRatePercent&gt;

Thank you very much for your help

Best Regards,
Sateesh

答案1

得分: 0

XSLT 2.0 中,您可以通过以下方式生成您展示的结果:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic">
<xsl:output indent="yes"/>

<xsl:template match="/">
    <ns1:DiscountInterestRatePercent xmlns:xslt-version="MonthlyDiscounts_XSLT_26/06/2022" xmlns:ns1="urn:siemens.com:Rates:SIE_I_T_S4_FIN1.0">
        <xsl:for-each-group select="//Obs" group-by="ObsDimension/@value">
            <Data>
                <Year>
                    <xsl:value-of select="substring-before(current-grouping-key(), '-')" />
                </Year>
                <Month>
                    <xsl:value-of select="substring-after(current-grouping-key(), '-')" />
                </Month>
                <RatePercentList>
                    <xsl:for-each select="current-group()">
                        <RatePercent>
                            <xsl:value-of select="ObsValue/@value" />
                        </RatePercent>
                    </xsl:for-each>
                </RatePercentList>
            </Data>
        </xsl:for-each-group>
    </ns1:DiscountInterestRatePercent>
</xsl:template>

</xsl:stylesheet>

补充:

如果要仅包括 SeriesKey 值为 &quot;ABZINS7&quot;Series 中的数据,请更改以下部分:

<xsl:for-each-group select="//Obs" group-by="ObsDimension/@value">

为:

<xsl:for-each-group select="//Series[SeriesKey/Value/@value='ABZINS7']/Obs" group-by="ObsDimension/@value">

尚未经过测试,因为您的示例不允许测试此条件。

英文:

In XSLT 2.0 you can produce the result you show by:

&lt;xsl:stylesheet version=&quot;2.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xpath-default-namespace=&quot;http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic&quot;&gt;
&lt;xsl:output indent=&quot;yes&quot;/&gt;

&lt;xsl:template match=&quot;/&quot;&gt;
    &lt;ns1:DiscountInterestRatePercent xmlns:xslt-version=&quot;MonthlyDiscounts_XSLT_26/06/2022&quot; xmlns:ns1=&quot;urn:siemens.com:Rates:SIE_I_T_S4_FIN1.0&quot;&gt;
		&lt;xsl:for-each-group select=&quot;//Obs&quot; group-by=&quot;ObsDimension/@value&quot;&gt;
			&lt;Data&gt;
				&lt;Year&gt;
					&lt;xsl:value-of select=&quot;substring-before(current-grouping-key(), &#39;-&#39;)&quot;/&gt;
				&lt;/Year&gt;
				&lt;Month&gt;
					&lt;xsl:value-of select=&quot;substring-after(current-grouping-key(), &#39;-&#39;)&quot;/&gt;
				&lt;/Month&gt;
				&lt;RatePercentList&gt;
					&lt;xsl:for-each select=&quot;current-group()&quot;&gt;
						&lt;RatePercent&gt;
							&lt;xsl:value-of select=&quot;ObsValue/@value&quot;/&gt;
						&lt;/RatePercent&gt;
					&lt;/xsl:for-each&gt;
				&lt;/RatePercentList&gt;
			&lt;/Data&gt;
		&lt;/xsl:for-each-group&gt;
    &lt;/ns1:DiscountInterestRatePercent&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

Added:

To include only data from Series whose SeriesKey value is &quot;ABZINS7&quot;, change:

&lt;xsl:for-each-group select=&quot;//Obs&quot; group-by=&quot;ObsDimension/@value&quot;&gt;

to:

&lt;xsl:for-each-group select=&quot;//Series[SeriesKey/Value/@value=&#39;ABZINS7&#39;]/Obs&quot; group-by=&quot;ObsDimension/@value&quot;&gt;

Untested, because your example does not allow testing of this condition.

huangapple
  • 本文由 发表于 2023年6月26日 22:58:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定