英文:
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">
<message:DataSet structureRef="BBK_SEDI" >
<generic:Series>
<generic:SeriesKey>
<generic:Value id="BBK_SEDI_CALCULATION" value="ABZINS7"></generic:Value>
</generic:SeriesKey>
<generic:Attributes>
<generic:Value id="BBK_ID" value="BBSDI.M.ABZINS7.R01"></generic:Value>
</generic:Attributes>
<generic:Obs>
<generic:ObsDimension value="2015-01"></generic:ObsDimension>
<generic:ObsValue value="3.07"></generic:ObsValue>
</generic:Obs>
<generic:Obs>
<generic:ObsDimension value="2016-01"></generic:ObsDimension>
<generic:ObsValue value="3.05"></generic:ObsValue>
</generic:Obs>
</generic:Series>
<generic:Series>
<generic:SeriesKey>
<generic:Value id="BBK_SEDI_CALCULATION" value="ABZINS7"></generic:Value>
</generic:SeriesKey>
<generic:Attributes>
<generic:Value id="BBK_ID" value="BBSDI.M.ABZINS7.R02"></generic:Value>
</generic:Attributes>
<generic:Obs>
<generic:ObsDimension value="2015-01"></generic:ObsDimension>
<generic:ObsValue value="1.07"></generic:ObsValue>
</generic:Obs>
<generic:Obs>
<generic:ObsDimension value="2016-01"></generic:ObsDimension>
<generic:ObsValue value="1.05"></generic:ObsValue>
</generic:Obs>
</generic:Series>
</message:DataSet>
</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'">
<Data>
<Year>
<xsl:value-of select="substring-before(./generic:Obs/generic:ObsDimension/@value,'-')"/>
</Year>
<Month>
<xsl:value-of select="substring-after(./generic:Obs/generic:ObsDimension/@value,'-')"/>
</Month>
<RatePercentList>
<xsl:for-each select="./generic:Obs" >
<RatePercent>
<xsl:value-of select="./generic:ObsValue/@value"/>
</RatePercent>
</xsl:for-each>
</RatePercentList>
</Data>
</xsl:if>
</xsl:for-each>
英文:
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
<?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">
<message:DataSet structureRef="BBK_SEDI" >
<generic:Series>
<generic:SeriesKey>
<generic:Value id="BBK_SEDI_CALCULATION" value="ABZINS7"></generic:Value>
</generic:SeriesKey>
<generic:Attributes>
<generic:Value id="BBK_ID" value="BBSDI.M.ABZINS7.R01"></generic:Value>
</generic:Attributes>
<generic:Obs>
<generic:ObsDimension value="2015-01"></generic:ObsDimension>
<generic:ObsValue value="3.07"></generic:ObsValue>
</generic:Obs>
<generic:Obs>
<generic:ObsDimension value="2016-01"></generic:ObsDimension>
<generic:ObsValue value="3.05"></generic:ObsValue>
</generic:Obs>
</generic:Series>
<generic:Series>
<generic:SeriesKey>
<generic:Value id="BBK_SEDI_CALCULATION" value="ABZINS7"></generic:Value>
</generic:SeriesKey>
<generic:Attributes>
<generic:Value id="BBK_ID" value="BBSDI.M.ABZINS7.R02"></generic:Value>
</generic:Attributes>
<generic:Obs>
<generic:ObsDimension value="2015-01"></generic:ObsDimension>
<generic:ObsValue value="1.07"></generic:ObsValue>
</generic:Obs>
<generic:Obs>
<generic:ObsDimension value="2016-01"></generic:ObsDimension>
<generic:ObsValue value="1.05"></generic:ObsValue>
</generic:Obs>
</generic:Series>
</message:DataSet>
</message:GenericData>
My XSLT provides gives the values from same arrays not from different arrays
**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'">
<Data>
<Year>
<xsl:value-of select="substring-before(./generic:Obs/generic:ObsDimension/@value,'-')"/>
</Year>
<Month>
<xsl:value-of select="substring-after(./generic:Obs/generic:ObsDimension/@value,'-')"/>
</Month>
<RatePercentList>
<xsl:for-each select="./generic:Obs" >
<RatePercent>
<xsl:value-of select="./generic:ObsValue/@value"/>
</RatePercent>
</xsl:for-each>
</RatePercentList>
</Data>
</xsl:if>
</xsl:for-each>
</ns1:DiscountInterestRatePercent>
</xsl:template>
</xsl:stylesheet>
Expected output
I am expecting an output based upon below format
<ns1:DiscountInterestRatePercent xmlns:xslt-version="MonthlyDiscounts_XSLT_26/06/2022"
xmlns:ns1="urn:ates:SI">
<Data>
<Year>2015</Year>
<Month>01</Month>
<RatePercentList>
<RatePercent>3.07</RatePercent>
<RatePercent>1.07</RatePercent>
</RatePercentList>
</Data>
<Data>
<Year>2016</Year>
<Month>01</Month>
<RatePercentList>
<RatePercent>3.05</RatePercent>
<RatePercent>1.05</RatePercent>
</RatePercentList>
</Data>
</ns1:DiscountInterestRatePercent>
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
值为 "ABZINS7"
的 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:
<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>
Added:
To include only data from Series
whose SeriesKey
value is "ABZINS7"
, change:
<xsl:for-each-group select="//Obs" group-by="ObsDimension/@value">
to:
<xsl:for-each-group select="//Series[SeriesKey/Value/@value='ABZINS7']/Obs" group-by="ObsDimension/@value">
Untested, because your example does not allow testing of this condition.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论