样式表
<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>