How to remve Namspaces from xml documents for xmlports

ara3nara3n Member Posts: 9,255
edited 2011-10-20 in NAV Tips & Tricks
Hello.
Here is an example on how to remove namespaces, instead of the approach MS has recommended.

Here is xml example.
<?xml version="1.0" encoding="UTF-16" standalone="no" ?> 
- <Mytest xmlns="http://www.w3.org/HTML/1998/html">
  <String>tada</String> 
  </Mytest>

In above example I want to remove
xmlns="http://www.w3.org/HTML/1998/html";

To create the xml file. simply run the report and select export. It will create the xml file. Then open it in notepad and add the above namespace to it.

Then run the report and and select the modified xml file with namespace.
OBJECT Report 50000 XML Import
{
  OBJECT-PROPERTIES
  {
    Date=02/17/07;
    Time=[ 8:47:52 PM];
    Modified=Yes;
    Version List=Example;
  }
  PROPERTIES
  {
    ProcessingOnly=Yes;
    OnInitReport=BEGIN
                   FileName := 'c:\example.xml';
                 END;

    OnPostReport=BEGIN
                   Process;
                   MESSAGE('finished');
                 END;

  }
  DATAITEMS
  {
  }
  REQUESTFORM
  {
    PROPERTIES
    {
      Width=13090;
      Height=3520;
      SaveValues=Yes;
    }
    CONTROLS
    {
      { 1   ;TextBox      ;3520 ;330  ;9350 ;440  ;AssistEdit=Yes;
                                                   SourceExpr=FileName;
                                                   OnAssistEdit=VAR
                                                                  FileDialog@1000000000 : Codeunit 412;
                                                                BEGIN
                                                                  FileName := FileDialog.OpenFile('Hello ' + USERID + ' Please Select the XML File.','',4,'xml',0);
                                                                END;
                                                                 }
      { 1000000003;Label  ;220  ;330  ;3300 ;440  ;CaptionML=ENU=File Name }
      { 1000000000;CheckBox;3850;1100 ;440  ;440  ;ShowCaption=No;
                                                   SourceExpr=Export }
      { 1000000001;Label  ;110  ;1100 ;3300 ;440  ;CaptionML=ENU=Export }
    }
  }
  CODE
  {
    VAR
      Export@1000000000 : Boolean;
      FileName@1000000005 : Text[100];
      Batch@1000000001 : Code[20];
      Mytextline@1000000002 : Text[1000];
      TempBlob@1000000003 : TEMPORARY Record 99008535;
      BinaryLine@1000000004 : Binary[1000];
      Char@1000000006 : Char;
      I@1000000007 : Integer;
      NameSP@1000000008 : Text[100];
      boo@1000000010 : Boolean;

    PROCEDURE Process@1000000001();
    VAR
      myxml@1000000003 : XMLport 50000;
      myfile@1000000002 : File;
      Istream@1000000001 : InStream;
      Ostream@1000000000 : OutStream;
    BEGIN
      IF Export THEN BEGIN
       IF EXISTS(FileName) THEN
         ERASE(FileName);

         myfile.CREATE(FileName);
         myfile.CREATEOUTSTREAM(Ostream);
         myxml.SETDESTINATION(Ostream);
         myxml.EXPORT;
         myfile.CLOSE;

      END ELSE BEGIN
       TempBlob.Blob.CREATEOUTSTREAM(Ostream);
       myfile.OPEN(FileName);
       NameSP := 'xmlns="http://www.w3.org/HTML/1998/html"';
       REPEAT
         CLEAR(BinaryLine);
         CLEAR(Mytextline);
         myfile.READ(BinaryLine);
         FOR I := 1 TO MAXSTRLEN(Mytextline) DO BEGIN
          Char := BinaryLine[I];
          Mytextline := Mytextline + FORMAT(Char);
         END;
         //this is where I search for NameSP and delete it.
         IF STRPOS(Mytextline,NameSP) <> 0 THEN
           Mytextline := DELSTR(Mytextline,STRPOS(Mytextline,NameSP),STRLEN(NameSP));

         CLEAR(BinaryLine);
         FOR I := 1 TO STRLEN(Mytextline) DO BEGIN
          Char := 0;
          Ostream.WRITE(Mytextline[I]);
          IF boo THEN BEGIN
            Ostream.WRITE(Char);
          END ELSE
            IF I > 2 THEN BEGIN
             Ostream.WRITE(Char);
             boo := TRUE;
          END;
         END;
       UNTIL myfile.LEN = myfile.POS;
       TempBlob.Blob.EXPORT('C:\newxml.xml',FALSE);
       CLEAR(Istream);
       TempBlob.Blob.CREATEINSTREAM(Istream);

       myxml.SETSOURCE(Istream);

       myxml.IMPORT;
       myfile.CLOSE;

      END;
    END;

    BEGIN
    END.
  }
}

OBJECT XMLport 50000 CustomsLink 7501
{
  OBJECT-PROPERTIES
  {
    Date=02/16/07;
    Time=[ 8:14:21 PM];
    Modified=Yes;
    Version List=Example;
  }
  PROPERTIES
  {
  }
  ELEMENTS
  {
    { [{4A7B5064-43B7-4F89-819C-5E9BCB3842C4}];  ;Mytest              ;Element ;Text    ;
                                                  VariableName=Mytest }

    { [{51DE76D0-B5D7-491F-AEE1-A1B3D6E308C6}];1 ;String              ;Element ;Text    ;
                                                  VariableName=String;
                                                  Export::OnBeforePassVariable=BEGIN
                                                                                 String := 'tada';
                                                                               END;
                                                                                }

  }
  EVENTS
  {
  }
  CODE
  {

    BEGIN
    END.
  }
}

Ahmed Rashed Amini
Independent Consultant/Developer


blog: https://dynamicsuser.net/nav/b/ara3n

Comments

  • kinekine Member Posts: 12,562
    Or you can use the How-To from partnersource (https://mbs.microsoft.com/partnersource ... rvices.htm) and do it in this way:
    How to use XMLports to connect to web services
    Navision 4.00 and XML namespaces
    Last Modified 12.7.2006
    Posted 29.1.2006
    Article ID
    This article describes how to handle namespaces in XMLports in Navision 4.00.



    On this page:

    Overview
    Technical Information
    Support Information

    Overview

    A web service is a programmatic interface for application to application communication through the internet. The communication protocol is based on XML documents. An XML document usually contains namespaces to qualify its element and attribute names. Both applications must support namespaces to communicate.



    XMLports in Navision can be used to generate XML documents and communicate with web services. But they do not support namespaces in version 4.00. A simple method is described in this article to get around this limitation.





    Technical Information

    To communicate with a web service, XMLports in Navision need to send and receive XML documents containing namespaces.

    1. Sending XML documents

    In the XMLport, the TagType “Attribute” can be used to define XML namespaces.

    For example, add the following tag in an XMLport:

    TagName: xmlns
    TagType: Attribute
    SourceType: Text
    DataSource: xmlns1

    Add the following C/AL code line in the OnPreXMLport() trigger of the XMLport:

    xmlns1 := 'http://schemas.xmlsoap.org/soap/envelope/';

    2. Receiving XML documents

    Before processing an XML document with an XMLport, an XML stylesheet can be applied to remove namespaces. An XML stylesheet contains a language for transforming XML documents into other XML documents.

    For example, the following procedure can be used in Navision:

    RemoveNamespace(
    XMLSourceDocument : Automation "'Microsoft XML, v4.0'.DOMDocument40";
    VAR XMLDestinationDocument : Automation "'Microsoft XML, v4.0'.DOMDocument40")

    TempTable."BLOB Field".CREATEOUTSTREAM(OutStreamStylesheet);
    TempTable."BLOB Field".CREATEINSTREAM(InStreamStylesheet);
    OutStreamStylesheet.WRITETEXT('<?xml version="1.0" encoding="UTF-8"?>');
    OutStreamStylesheet.WRITETEXT('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">');
    OutStreamStylesheet.WRITETEXT('<xsl:output method="xml" encoding="UTF-8" />');
    OutStreamStylesheet.WRITETEXT('<xsl:template match="/">');
    OutStreamStylesheet.WRITETEXT('<xsl:copy>');
    OutStreamStylesheet.WRITETEXT('<xsl:apply-templates />');
    OutStreamStylesheet.WRITETEXT('</xsl:copy>');
    OutStreamStylesheet.WRITETEXT('</xsl:template>');
    OutStreamStylesheet.WRITETEXT('<xsl:template match="*">');
    OutStreamStylesheet.WRITETEXT('<xsl:element name="{local-name()}">');
    OutStreamStylesheet.WRITETEXT('<xsl:apply-templates select="@* | node()" />');
    OutStreamStylesheet.WRITETEXT('</xsl:element>');
    OutStreamStylesheet.WRITETEXT('</xsl:template>');
    OutStreamStylesheet.WRITETEXT('<xsl:template match="@*&quot;>');
    OutStreamStylesheet.WRITETEXT('<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>');
    OutStreamStylesheet.WRITETEXT('</xsl:template>');
    OutStreamStylesheet.WRITETEXT('<xsl:template match="text() | processing-instruction() | comment()">');
    OutStreamStylesheet.WRITETEXT('<xsl:copy />');
    OutStreamStylesheet.WRITETEXT('</xsl:template>');
    OutStreamStylesheet.WRITETEXT('</xsl:stylesheet>');
    IF ISCLEAR(XMLStyleSheet) THEN
    CREATE(XMLStyleSheet);
    XMLStyleSheet.load(InStreamStylesheet);
    IF ISCLEAR(XMLDestinationDocument) THEN
    CREATE(XMLDestinationDocument);
    XMLSourceDocument.transformNodeToObject(XMLStyleSheet,XMLDestinationDocument);

    Add the following C/AL Locals in the procedure:

    TempTable: Record
    OutStreamStylesheet: OutStream
    InStreamStylesheet: InStream
    XMLStyleSheet: Automation "'Microsoft XML, v4.0'.DOMDocument40"





    Support Information

    Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and the tools that are used to create and debug procedures.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • tuborgtuborg Member Posts: 6
    Thanks!
    It worked directly!

    =D>
Sign In or Register to comment.