mibuso.com

Microsoft Business Solutions online community
It is currently Mon Dec 22, 2014 8:01 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to remve Namspaces from xml documents for xmlports
PostPosted: Sun Feb 18, 2007 2:49 am 
Offline
MVP Microsoft Dynamics NAV

Joined: Wed Dec 15, 2004 5:11 pm
Posts: 9075
Location: 3rd rock from sun
Country: United States (us)
Hello.
Here is an example on how to remove namespaces, instead of the approach MS has recommended.

Here is xml example.

Code: Select all
<?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.

Code: Select all
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.
  }
}


_________________
Rashed.

blog: http://mibuso.com/blogs/ara3n/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 1:38 pm 
Online
MVP Microsoft Dynamics NAV
NAV TechDays 2014 attendee

Joined: Thu Oct 16, 2003 7:50 am
Posts: 12443
Location: Brno
Country: Czech Republic (cz)
Or you can use the How-To from partnersource (https://mbs.microsoft.com/partnersource ... rvices.htm) and do it in this way:
Quote:
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="@*">');
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.


Top
 Profile E-mail WWW  
 
 Post subject: Re: How to remve Namspaces from xml documents for xmlports
PostPosted: Thu Oct 20, 2011 2:33 pm 
Offline

Joined: Mon Jun 05, 2006 1:35 pm
Posts: 4
Location: Helsingborg
Country: Sweden (se)
Thanks!
It worked directly!

=D>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum


Search for:
Jump to: