0

I've created an XML file, but I need to change the order of elements. I need that first element under Invoice be cec:UBLExtensions with child elements (than cbc:CustomizationID and other in same order) My XML file is:

-<Invoice>
<cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.rs:srbdt:2021</cbc:CustomizationID>
<cbc:ID>701708-2022</cbc:ID>
<cbc:IssueDate>2022-11-06</cbc:IssueDate>
<cbc:DueDate>2022-11-21</cbc:DueDate>
<cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
<cbc:Note>Nema oslobadjanja dobara od PDV-a</cbc:Note>
<cbc:DocumentCurrencyCode>RSD</cbc:DocumentCurrencyCode>
-<cec:UBLExtensions>
-<cec:UBLExtension>
-<cec:ExtensionContent>
</cec:ExtensionContent>
</cec:UBLExtension>
</cec:UBLExtensions>
-<cac:InvoicePeriod>
<cbc:DescriptionCode>35</cbc:DescriptionCode>
</cac:InvoicePeriod>
</Invoice>

I tried:

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

  <xsl:template match="Invoice">
    <xsl:copy>
      <xsl:apply-templates select="* except (cbc:CustomizationID,
        cbc:ID, cbc:IssueDate, cbc:DueDate, cbc:InvoiceTypeCode, cbc:DocumentCurrencyCode, cec:UBLExtensions, cac:InvoicePeriod)"/>
      <xsl:apply-templates select="cec:UBLExtensions, cbc:CustomizationID,
        cbc:ID, cbc:IssueDate, cbc:DueDate, cbc:InvoiceTypeCode, cbc:DocumentCurrencyCode,cac: InvoicePeriod"/>
    </xsl:copy>
  </xsl:template>

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

How it is possible with XSLT?
Ken White
  • 123,280
  • 14
  • 225
  • 444
Dusko
  • 1
  • 3
  • Yes, it's possible with XSLT. Yes, someone likely can help you. No, nobody likely will write code to your requirements without you trying first and posting a [mcve] illustrating where you're stuck. (Be sure to include [*namespace-well-formed*](https://stackoverflow.com/a/25830482/290085) input XML --what you've posted is not.) Please read [ask]. – kjhughes Nov 06 '22 at 22:00
  • Now that you have added your XSLT, how about also showing us the result you want and the wrong result you got, together with telling us which XSLT processor you used? And of course, if part of the wanted order is as used in the (second!) `xs:apply-templates` with e.g. `cec:UBLExtensions, cbc:CustomizationID`, why are you inserting any other elements before that with the first `xsl:apply-templates`? – Martin Honnen Nov 06 '22 at 22:39
  • Nothing has changed. My xml file remained the same – Dusko Nov 06 '22 at 22:50
  • I would like code: ```` - - - urn:cen.eu:en16931:2017#compliant# 701708-2022 2022-11-06 2022-11-21 380 nn RSD - 35 ```` – Dusko Nov 06 '22 at 23:28
  • If the code as posted did not change anything then make sure you show us the real source code of the input XML, including any namespace declarations. Also tell us the XSLT processor and version you use. – Martin Honnen Nov 07 '22 at 08:43
  • It is also highly unlikely that the posted XSLT did not just give an error as using qualified names with prefixes like e.g. `cec:UBLExtensions` in an XPath expression can only work if the stylesheet declares a namespace for the used prefix e.g. `xmlns:cec="http://example.com/cec"`, otherwise you should kind of get a compilation error. – Martin Honnen Nov 07 '22 at 08:45

1 Answers1

0

This is done in Visual Studio 1.0. So make adjustments to declarations as needed.

Define your namespaces in the XML.

<Invoice xmlns:cbc="http://youdefine1" xmlns:cec="http://youdefine2" xmlns:cac="http://youdefine3">
  <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.rs:srbdt:2021</cbc:CustomizationID>
  <cbc:ID>701708-2022</cbc:ID>
  <cbc:IssueDate>2022-11-06</cbc:IssueDate>  
  <cbc:DueDate>2022-11-21</cbc:DueDate>
  <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
  <cbc:Note>Nema oslobadjanja dobara od PDV-a</cbc:Note>
  <cbc:DocumentCurrencyCode>RSD</cbc:DocumentCurrencyCode>
  <cec:UBLExtensions>
    <cec:UBLExtension>
      <cec:ExtensionContent>
      </cec:ExtensionContent>
   </cec:UBLExtension>
  </cec:UBLExtensions>
  <cac:InvoicePeriod>
    <cbc:DescriptionCode>35</cbc:DescriptionCode>
  </cac:InvoicePeriod>
</Invoice>

Then here is the XSLT code to reorder you XML nodes.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cbc="http://youdefine1"
    xmlns:cec="http://youdefine2"
    xmlns:cac="http://youdefine3">

  <xsl:template match="Invoice">
    <xsl:copy>
      <xsl:apply-templates select="cec:UBLExtensions"/>
      <xsl:apply-templates select="cbc:CustomizationID"/>
      <xsl:apply-templates select="*[name() != 'cec:UBLExtensions' and name() != 'cbc:CustomizationID']"/>
    </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>
John Ernst
  • 1,206
  • 1
  • 7
  • 11