Я пытаюсь написать файл xsl, чтобы облегчить импорт файла xml в базу данных. Поэтому мне нужно, чтобы идентификатор родительского узла был скопирован на дочерние узлы, чтобы они служили внешним ключом после импорта.

Входной XML:

<?xml version="1.0" encoding="utf-8"?>
<RequestedDoc xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0" xmlns="http://www.aade.gr/myDATA/invoice/v1.0">
  <invoicesDoc>
    <invoice>
      <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
      <mark>400000090600432</mark>
      <issuer>
        <vatNumber>117570526</vatNumber>
        <country>GR</country>
        <branch>0</branch>
      </issuer>
      <counterpart>
        <vatNumber>090027010</vatNumber>
        <country>GR</country>
        <branch>0</branch>
      </counterpart>
      <invoiceHeader>
        <series>0</series>
        <aa>1</aa>
        <issueDate>2020-10-05</issueDate>
        <invoiceType>1.1</invoiceType>
        <vatPaymentSuspension>false</vatPaymentSuspension>
        <currency>EUR</currency>
      </invoiceHeader>
      <paymentMethods>
        <paymentMethodDetails>
          <type>3</type>
          <amount>1</amount>
        </paymentMethodDetails>
      </paymentMethods>
      <invoiceDetails>
        <lineNumber>1</lineNumber>
        <netValue>1</netValue>
        <vatCategory>7</vatCategory>
        <vatAmount>0</vatAmount>
        <vatExemptionCategory>8</vatExemptionCategory>
        <discountOption>true</discountOption>
        <incomeClassification>
          <icls:classificationType>E3_561_001</icls:classificationType>
          <icls:classificationCategory>category1_3</icls:classificationCategory>
          <icls:amount>1</icls:amount>
        </incomeClassification>
      </invoiceDetails>
      <invoiceSummary>
        <totalNetValue>1</totalNetValue>
        <totalVatAmount>0</totalVatAmount>
        <totalWithheldAmount>0</totalWithheldAmount>
        <totalFeesAmount>0</totalFeesAmount>
        <totalStampDutyAmount>0</totalStampDutyAmount>
        <totalOtherTaxesAmount>0</totalOtherTaxesAmount>
        <totalDeductionsAmount>0</totalDeductionsAmount>
        <totalGrossValue>1</totalGrossValue>
        <incomeClassification>
          <icls:classificationType>E3_561_001</icls:classificationType>
          <icls:classificationCategory>category1_3</icls:classificationCategory>
          <icls:amount>1</icls:amount>
        </incomeClassification>
      </invoiceSummary>
    </invoice>
  </invoicesDoc>
</RequestedDoc>

Я пытаюсь получить такой результат:

<?xml version="1.0" encoding="utf-8"?>
<RequestedDoc xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0" xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0" xmlns="http://www.aade.gr/myDATA/invoice/v1.0">
  <invoicesDoc>
    <invoice>
      <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
      <mark>400000090600432</mark>
      <issuer>
        <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
        <vatNumber>117570526</vatNumber>
        <country>GR</country>
        <branch>0</branch>
      </issuer>
      <counterpart>
        <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
        <vatNumber>090027010</vatNumber>
        <country>GR</country>
        <branch>0</branch>
      </counterpart>
      <invoiceHeader>
        <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
        <series>0</series>
        <aa>1</aa>
        <issueDate>2020-10-05</issueDate>
        <invoiceType>1.1</invoiceType>
        <vatPaymentSuspension>false</vatPaymentSuspension>
        <currency>EUR</currency>
      </invoiceHeader>
      <paymentMethods>
        <paymentMethodDetails>
          <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
          <type>3</type>
          <amount>1</amount>
        </paymentMethodDetails>
      </paymentMethods>
      <invoiceDetails>
        <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
        <lineNumber>1</lineNumber>
        <netValue>1</netValue>
        <vatCategory>7</vatCategory>
        <vatAmount>0</vatAmount>
        <vatExemptionCategory>8</vatExemptionCategory>
        <discountOption>true</discountOption>
        <incomeClassification>
          <icls:classificationType>E3_561_001</icls:classificationType>
          <icls:classificationCategory>category1_3</icls:classificationCategory>
          <icls:amount>1</icls:amount>
        </incomeClassification>
      </invoiceDetails>
      <invoiceSummary>
        <uid>F2D232C83627739466261DBD96A355074E7FE591</uid>
        <totalNetValue>1</totalNetValue>
        <totalVatAmount>0</totalVatAmount>
        <totalWithheldAmount>0</totalWithheldAmount>
        <totalFeesAmount>0</totalFeesAmount>
        <totalStampDutyAmount>0</totalStampDutyAmount>
        <totalOtherTaxesAmount>0</totalOtherTaxesAmount>
        <totalDeductionsAmount>0</totalDeductionsAmount>
        <totalGrossValue>1</totalGrossValue>
        <incomeClassification>
          <icls:classificationType>E3_561_001</icls:classificationType>
          <icls:classificationCategory>category1_3</icls:classificationCategory>
          <icls:amount>1</icls:amount>
        </incomeClassification>
      </invoiceSummary>
    </invoice>
  </invoicesDoc>
</RequestedDoc>

Для этого я написал такой код:

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

    <xsl:output indent="yes"/>  
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="issuer | counterpart | invoiceHeader | paymentMethods/paymentMethodDetails | invoiceDetails | invoiceSummary">
        <issuer>
            <uid><xsl:value-of select="/RequestedDoc/invoicesDoc/invoice/uid"/></uid>
            <xsl:apply-templates/>
        </issuer>
    </xsl:template>
    
</xsl:stylesheet>

Но не работает. Кто-нибудь может помочь?

0
gpkintsakis 8 Окт 2020 в 15:17

1 ответ

Лучший ответ

Отсутствовала обработка пространства имен по умолчанию. Я также упростил ваш второй шаблон.

Попробуйте следующий XSLT.

< Сильный > XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.aade.gr/myDATA/invoice/v1.0" xmlns:a="http://www.aade.gr/myDATA/invoice/v1.0" xmlns:icls="https://www.aade.gr/myDATA/incomeClassificaton/v1.0"
                xmlns:ecls="https://www.aade.gr/myDATA/expensesClassificaton/v1.0" exclude-result-prefixes="a">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="a:issuer | a:counterpart | a:invoiceHeader | a:paymentMethodDetails | a:invoiceDetails | a:invoiceSummary">
        <xsl:copy>
         <xsl:copy-of select="/a:RequestedDoc/a:invoicesDoc/a:invoice/a:uid"/>
         <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
2
Yitzhak Khabinsky 8 Окт 2020 в 13:07