迭代元素名称中的数字

时间:2013-08-16 11:33:50

标签: xml xslt xpath

我想在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。

我无法找到如何在元素名称中迭代数字。 请帮帮忙?

干杯,

聪岛

1 个答案:

答案 0 :(得分:4)

样式表

<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>

Saxon 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>