gpt4 book ai didi

xml - 遍历元素名称中的数字

转载 作者:行者123 更新时间:2023-12-03 17:05:02 25 4
gpt4 key购买 nike

我想在XSLT中创建一个循环,该循环可以转换以下XML:

<PAYMENTS>
<PAYMENT_BATCH_ID>987654321</PAYMENT_BATCH_ID>
<PAYMENT_NUMBER1_AMOUNT>123456789</PAYMENT_NUMBER1_AMOUNT>
<PAYMENT_NUMBER1_CURRENCY>EUR</PAYMENT_NUMBER1_CURRENCY>
<PAYMENT_NUMBER1_DATE>19700101</PAYMENT_NUMBER1_DATE>
<PAYMENT_STRING1_DESCRIPTION>Description</PAYMENT_STRING1_DESCRIPTION>
<PAYMENT_NUMBER2_AMOUNT>123456789</PAYMENT_NUMBER2_AMOUNT>
<PAYMENT_NUMBER2_CURRENCY>GBP</PAYMENT_NUMBER2_CURRENCY>
<PAYMENT_NUMBER2_DATE>19700101</PAYMENT_NUMBER2_DATE>
<PAYMENT_STRING2_DESCRIPTION>Description</PAYMENT_STRING2_DESCRIPTION>
<PAYMENT_NUMBERn_AMOUNT>123456789</PAYMENT_NUMBERn_AMOUNT>
<PAYMENT_NUMBERn_CURRENCY>CHF</PAYMENT_NUMBERn_CURRENCY>
<PAYMENT_NUMBERn_DATE>19700101</PAYMENT_NUMBERn_DATE>
<PAYMENT_STRINGn_DESCRIPTION>Description</PAYMENT_STRINGn_DESCRIPTION>
</PAYMENTS>


放入此XML:

<PAYMENTS>
<PAYMENT>
<CURRENCY>EUR</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
<PAYMENT>
<CURRENCY>GBP</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
<PAYMENT>
<CURRENCY>CHF</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
</PAYMENTS>


请注意,n可以是任意数字,因此您可以具有任意数量的付款。我正在使用XSLT 2.0。

我无法找到如何遍历元素名称中的数字。
请帮忙?

干杯,

Tuno

最佳答案

样式表

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="PAYMENTS">
<xsl:copy>
<xsl:for-each-group select="*" group-by="replace(local-name(), '\D+', '')">
<PAYMENT>
<xsl:apply-templates select="current-group()"/>
</PAYMENT>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

<xsl:template match="PAYMENTS/*">
<xsl:element name="{substring-after(substring-after(local-name(), '_'), '_')}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>


转换

<PAYMENTS>
<PAYMENT_NUMBER1_CURRENCY>EUR</PAYMENT_NUMBER1_CURRENCY>
<PAYMENT_NUMBER1_AMOUNT>123456789</PAYMENT_NUMBER1_AMOUNT>
<PAYMENT_NUMBER1_DATE>19700101</PAYMENT_NUMBER1_DATE>
<PAYMENT_NUMBER2_CURRENCY>GBP</PAYMENT_NUMBER2_CURRENCY>
<PAYMENT_NUMBER2_AMOUNT>123456789</PAYMENT_NUMBER2_AMOUNT>
<PAYMENT_NUMBER2_DATE>19700101</PAYMENT_NUMBER2_DATE>
<PAYMENT_NUMBERn_CURRENCY>CHF</PAYMENT_NUMBERn_CURRENCY>
<PAYMENT_NUMBERn_AMOUNT>123456789</PAYMENT_NUMBERn_AMOUNT>
<PAYMENT_NUMBERn_DATE>19700101</PAYMENT_NUMBERn_DATE>
</PAYMENTS>


进入

<PAYMENTS>
<PAYMENT>
<CURRENCY>EUR</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
<PAYMENT>
<CURRENCY>GBP</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
<PAYMENT>
<CURRENCY>CHF</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
</PAYMENTS>


如果您不希望所有元素,并且想要更改输出中元素的顺序,则需要显式列出它们:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="PAYMENTS">
<xsl:copy>
<xsl:for-each-group select="* except PAYMENT_BATCH_ID" group-by="replace(local-name(), '\D+', '')">
<PAYMENT>
<xsl:apply-templates select="current-group()[self::*[contains(local-name(), 'CURRENCY')]],
current-group()[self::*[contains(local-name(), 'AMOUNT')]],
current-group()[self::*[contains(local-name(), 'DATE')]]"/>
</PAYMENT>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

<xsl:template match="PAYMENTS/*">
<xsl:element name="{substring-after(substring-after(local-name(), '_'), '_')}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>


使用该样式表和输入

<PAYMENTS>
<PAYMENT_BATCH_ID>987654321</PAYMENT_BATCH_ID>
<PAYMENT_NUMBER1_AMOUNT>123456789</PAYMENT_NUMBER1_AMOUNT>
<PAYMENT_NUMBER1_CURRENCY>EUR</PAYMENT_NUMBER1_CURRENCY>
<PAYMENT_NUMBER1_DATE>19700101</PAYMENT_NUMBER1_DATE>
<PAYMENT_STRING1_DESCRIPTION>Description</PAYMENT_STRING1_DESCRIPTION>
<PAYMENT_NUMBER2_AMOUNT>123456789</PAYMENT_NUMBER2_AMOUNT>
<PAYMENT_NUMBER2_CURRENCY>GBP</PAYMENT_NUMBER2_CURRENCY>
<PAYMENT_NUMBER2_DATE>19700101</PAYMENT_NUMBER2_DATE>
<PAYMENT_STRING2_DESCRIPTION>Description</PAYMENT_STRING2_DESCRIPTION>
<PAYMENT_NUMBERn_AMOUNT>123456789</PAYMENT_NUMBERn_AMOUNT>
<PAYMENT_NUMBERn_CURRENCY>CHF</PAYMENT_NUMBERn_CURRENCY>
<PAYMENT_NUMBERn_DATE>19700101</PAYMENT_NUMBERn_DATE>
<PAYMENT_STRINGn_DESCRIPTION>Description</PAYMENT_STRINGn_DESCRIPTION>
</PAYMENTS>


撒克逊9.5输出

<PAYMENTS>
<PAYMENT>
<CURRENCY>EUR</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
<PAYMENT>
<CURRENCY>GBP</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
<PAYMENT>
<CURRENCY>CHF</CURRENCY>
<AMOUNT>123456789</AMOUNT>
<DATE>19700101</DATE>
</PAYMENT>
</PAYMENTS>

关于xml - 遍历元素名称中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com