Options

XML Port with different order of elements

BikoBiko Member Posts: 2
edited 2015-11-03 in NAV Three Tier
Hi,
how can I read the xml with a different order of complex elements?
I have schema:
<xsd:element name="sheet">
<xsd:complexType>
<xsd:all> <- this is a key element of the problem
<xsd:element type="xsd:string" name="name"/>
<xsd:element type="xsd:string" name="salesman"/>
<xsd:element type="xsd:string" name="customer"/>
</xsd:all>
</xsd:complexType>
</xsd:element>

and in XML files:

<sheet>
<name>Value</name>
<salesman>Value</salesman>
<customer>Value</customer>
</sheet>

and sometimes:

<sheet>
<name>Value</name>
<customer>Value</customer>
<salesman>Value</salesman>
</sheet>

Comments

  • Options
    vaprogvaprog Member Posts: 1,118
    Hi,

    I suggest to use a transform in order to fix up the element order before feeding the document to the XMLport. I don't think XMLport can handle it otherwise.
  • Options
    BikoBiko Member Posts: 2
    I think there must be some system solution.
  • Options
    lephrolephro Member Posts: 2
    You can use the Codeunit 6224 "XML DOM Mangement" which contains functions to parse a xml document. It encapsulates some .NET functions and sometimes needs some extensions (depending on the complexity of your requirements). But its a good way to start.

    Of course you could also use the .NET Classes (System.Xml) right away to parse the xml file. You can use XPath do access the nodes directly in the order you need.

    You can find some examples in the Codeunit 7701"ADCS Communication" if youre not sure how to use the Codeunit / Classes.
  • Options
    SeeSharpSeeSharp Member Posts: 19
    edited 2015-11-03
    If you're to use an XML port, you're probably going to be best to just apply a transform against the XML doc. Just put all child nodes of 'sheet' in alphabetical order and set your XMLport up accordingly.
    Name	DataType	Subtype	Length
    xslCompTrans	DotNet	System.Xml.Xsl.XslCompiledTransform.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    
    
    xslCompTrans.Load('P:\ath\To\Stylesheet.xsl');
    xslCompTrans.Transform('T:\emp\LocationOfIncoming.xml', 'M:\y\Output.xml');
    
    // Call your XML port against 'Output.xml'
    

    Have your stylesheet.xsl look like this:
    <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="sheet">
        <sheet>
          <xsl:apply-templates>
            <xsl:sort select="name()"/>
          </xsl:apply-templates>
        </sheet>
      </xsl:template>
    </xsl:stylesheet>
    

    Cheers

    edit: there's a rogue semi colon appearing after the URL in the XML namespace. You won't need that.
Sign In or Register to comment.