将 XSLT 2.0 与没有索引的区段分组问题

grouping issue with my xslt 2.0 with segments that have no index

提问人:jimboextended 提问时间:10/24/2023 最后编辑:jimboextended 更新时间:10/24/2023 访问量:38

问:

我尝试完成以下结构:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Responses>
        <index>000010</index>
        <brutto>246.09</brutto>
        <bruttoSingle>246.09</bruttoSingle>
        <netto>7327.14</netto>
        <nettoSingle>244.24</nettoSingle>
        <logisticsFlatRatValue>18.27</<logisticsFlatRatValue>
        <logisticsFlatRatpercentage>0.25</logisticsFlatRatpercentage>
        <Currency>USD</Currency>
    </Responses>
    <Responses/>
    <Responses>
        <index>000020</index>
        <brutto>10.20</brutto>
        <bruttoSingle>10.20</bruttoSingle>
        <netto>2.88</netto>
        <nettoSingle>2.88</nettoSingle
        <logisticsFlatRatValue>18.27</<logisticsFlatRatValue>
        <logisticsFlatRatpercentage>0.25</logisticsFlatRatpercentage>
        <Currency>USD</Currency>
    </Responses>
    <Responses/>
</Response>

这是我的意见:

<Response>
    <Responses>
        <brutto>
            <indx>000010</indx>
            <value>246.09</value>
            <currency>USD</currency>
        </brutto>
        <bruttoSingle>
            <indx>000010</indx>
            <value>246.09</value>
            <currency>USD</currency>
        </bruttoSingle>
        <logisticsFlatRate>
            <value>18.27</value>
            <percentage>0.25</percentage>
        </logisticsFlatRate>
    </Responses>
    <Responses>
        <netto>
            <indx>000010</indx>
            <value>7327.14</value>
            <currency>USD</currency>
        </netto>
        <nettoSingle>
            <indx>000010</indx>
            <value>244.24</value>
            <currency>USD</currency>
        </nettoSingle>
    </Responses>
    <Responses/>
    <Responses>
        <brutto>
            <indx>000020</indx>
            <value>10.20</value>
            <currency>USD</currency>
        </brutto>
        <bruttoSingle>
            <indx>000020</indx>
            <value>10.20</value>
            <currency>USD</currency>
        </bruttoSingle>
    </Responses>
    <Responses>
        <netto>
            <indx>000020</indx>
            <value>2.88</value>
            <currency>USD</currency>
        </netto>
        <nettoSingle>
            <indx>000020</indx>
            <value>2.88</value>
            <currency>USD</currency>
        </nettoSingle>
        <logisticsFlatRate>
            <value>15.27</value>
            <percentage>0.45</percentage>
        </logisticsFlatRate>
    </Responses>
    <Responses/>
</Response>

到目前为止,我的 xslt:

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

    <xsl:template match="/">
        <xsl:for-each-group select="//Responses/*[index]" group-by="index">
            <group>
                <index><xsl:value-of select="current-grouping-key()"/></index>
                <items>
                    <xsl:for-each select="current-group()">
                        <xsl:element name="{name()}">
                            <xsl:value-of select="value"/>
                        </xsl:element>
                    </xsl:for-each>
                    <xsl:for-each select="/Response/Responses/logisticsFlatRate">
                        <xsl:copy-of select="."/>
                    </xsl:for-each>
                </items>
            </group>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

我知道 logistc flat 的这部分是错误的,我尝试使用上面的分组,但由于 logisticFlatRate 段中没有索引,因此它不起作用。

我在货币和物流上苦苦挣扎。我的目标是有一个扁平的结构,就像我上面的例子一样

XML XSLT XSD

评论

0赞 michael.hor257k 10/24/2023
请将代码减少到演示问题所需的最低限度,并用文字解释所需的逻辑。
0赞 Michael Kay 10/24/2023
是错别字,还是您正在寻找的错误?indexindx
0赞 jimboextended 10/30/2023
@MichaelKay不,这只是一个错别字

答:

1赞 Michael Kay 10/24/2023 #1

此代码:

<xsl:for-each select="/Response/Responses/logisticsFlatRate">
    <xsl:copy-of select="."/>
</xsl:for-each>

显然是错误的,因为前导“/”是从文档的根目录中选择的,而这里没有说明您要复制的内容。就此而言,您还没有告诉我们足够的问题,让我们无法回答这个问题。但是,鉴于您的示例数据,您可能能够使用logisticsFlatRate

<xsl:apply-templates select="current-group()/following-sibling::logisticsFlatRate"/>

加上一个适当的模板规则,用于将 转换为输出中实际想要的两个元素。logisticsFlatRate

评论

0赞 jimboextended 10/30/2023
谢谢!我添加了这个模板规则,现在一切都按预期工作 ''' <xsl:template match=“logisticsFlatRate”> <logisticsFlatrateValue> <xsl:value-of select=“value”/> </logisticsFlatrateValue> <logisticsFlatRatePercentage> <xsl:value-of select=“percentage”/> </logisticsFlatRatePercentage> </xsl:template> '''