xmlStripNameSpaces ( )

Function stats

Average user rating
602
418
9999
Support
FileMaker 10.0 +
Date posted
27 January 2019
Last updated
28 January 2019
Version
Recursive function
Yes

Author Info
 Kevin Lietaer

1 functions

Average Rating None

author_avatar



 

Function overview

Prototype

xmlStripNameSpaces  ( _XML )


Parameters

_XML  XML Formatted text containing namespaces


Description

Tags:  XML  

strip ALL namespaces from XML

Examples

Sample input

"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
    <soap:Body><ProcessSalesInvoicesResponse xmlns=\"http://awebsitewithmoreinformation.com/\"><ProcessSalesInvoicesResult>
    <SalesInvoicesImportResponse xmlns=\"urn:xmlns:http://awebsitewithmoreinformation.com:processsalesinvoice\"><TimeStamp xmlns=\"\">2019-01-25</TimeStamp>
    <Succeeded xmlns=\"\">2</Succeeded><Failed xmlns=\"\">0</Failed><Skipped xmlns=\"\">0</Skipped>
    <Invoice xmlns=\"\"><Succeeded>true</Succeeded><Processed>true</Processed><Reference>XXXXXXX-1</Reference><Contact>XXXXXX 1</Contact></Invoice>
    <Invoice xmlns=\"\"><Succeeded>true</Succeeded><Processed>true</Processed><Reference>XXXXXXX-2</Reference><Contact>XXXXXX 2</Contact></Invoice>
    </SalesInvoicesImportResponse></ProcessSalesInvoicesResult></ProcessSalesInvoicesResponse></soap:Body></soap:Envelope>"


Sample output

<Envelope>     <Body><ProcessSalesInvoicesResponse><ProcessSalesInvoicesResult>     <SalesInvoicesImportResponse><TimeStamp>2019-01-25</TimeStamp>     <Succeeded>2</Succeeded><Failed>0</Failed><Skipped>0</Skipped>     <Invoice><Succeeded>true</Succeeded><Processed>true</Processed><Reference>XXXXXXX-1</Reference><Contact>XXXXXX 1</Contact></Invoice>     <Invoice><Succeeded>true</Succeeded><Processed>true</Processed><Reference>XXXXXXX-2</Reference><Contact>XXXXXX 2</Contact></Invoice>     </SalesInvoicesImportResponse></ProcessSalesInvoicesResult></ProcessSalesInvoicesResponse></Body></Envelope>

 

Function code

//    Custom Function: xmlStripNameSpaces ( _XML )
//    Version: 1.1 ( 27/01/2018 )
//    Created by: Kevin Lietaer
//    Created with: FileMaker Pro 17 Advanced
//    Dependencies: Position, Middle, Substitute ( FM Native functions )
//    Purpose: strip ALL namespaces from XML
//    Check below the script for additional information/remarks

Let (
    posXMLNS = Position ( _XML ; " xmlns" ; 1 ; 1 );
        // Find the position of the xlmns ( include the leading space, as otherwise it will clutter your xml with unnecessary whitespaces )
    Case (
        not posXMLNS;
            _XML;
                // As per definition, Position will return zero when the value is not found. When no namespace is present, simply return the XML
                // A side-effect of this approach is the when an empty XML is send, it will directly exit without an extra test
        Let ( [
                posXMLNSNextChar = Middle ( _XML ; posXMLNS + 6 ; 1 );
                    // I use + 6 here, as " xmlns" take up 6 characters
                posXMLNSPrefixEnd = If (
                    posXMLNSNextChar = ":" ;
                        // As per XML definition, a : after the declaration of a namespace assigns a prefix for use within the XML document
                        Position ( _XML ; "=" ; posXMLNS ; 1 )
                            // As a result, we can use the syntax to determine the endposition of the prefix
                );
                XMLNSPrefix = If (
                    posXMLNSPrefixEnd ;
                        Middle ( _XML ; posXMLNS + 7 ; posXMLNSPrefixEnd - posXMLNS - 7 )
                            // Note that we do not use + or - 6, but 7, as the ":" takes a character as well
                );
                posXMLNSEnd = Position ( _XML ; "\"" ; posXMLNS ; 2 );
                    // A namespace should be between double quotes, so the second double quote indicates the end
                XMLNSLength = posXMLNSEnd - posXMLNS + 1;
                XMLNS = Middle ( _XML ; posXMLNS ; XMLNSLength );
                    // We now have the exact used syntax of the namespace, including the prefix, uri and brackets
                _XML = Substitute ( _XML ; XMLNS ; "" );
                    // We have now removed the used syntax
                _XML = If ( not IsEmpty (XMLNSPrefix) ;
                    Substitute ( _XML ; "<" & XMLNSPrefix & ":" ; "<" ) ; _XML
                ); // When a prefix is used, remove all references to the namespace from the start tags
                _XML = If ( not IsEmpty (XMLNSPrefix) ;
                    Substitute ( _XML ; "</" & XMLNSPrefix & ":" ; "</" ) ; _XML
                ) // When a prefix is used, remove all references to the namespace from the end tags
            ];
            xmlStripNameSpaces ( _XML )
                // As there may be any number of namespaces used in an XML document, create a loop untill no more namespaces are found
        )
    )
)

/*
    * Remarks - flaws
    - All comments, bugreports, improvements and suggestions are appreciated, as long as you stay polite ;);
    - I didn't account for prefixes that are not declared correctly using the xmlns declaration;
    - I didn't account for extra spaces as I'm expecting a correctly formed XML, which shouldn't include extra spaces where they aren't expected;
    - This was created and tested in FileMaker 17 Pro Advanced but should work in any version, within dependancies
    - I hope this can be useful to someone other than myself;
    - I do not create code/functions as a professional, although I use it professionally;

    * XML Namespaces - What do you need to know ?
    
    First of all, namespaces are used to prevent element name conflicts while interacting with XML.
    They are defined within the <root> tag of the body or within other elements. They can be declared in 2 ways:
        xmlns="{URI}" or xmlns:{prefix}="{URI}"
    The prefix can then be used before XML tags: ( <{prefix}:{tagname}>
    The most common use for namespaces is for SOAP and transforming XML into HTML using XSLT.
    More information on XML Namespace can be found on several websites, including: https://www.w3schools.com/xml/xml_namespaces.asp    
*/

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/418

    Prototype: xmlStripNameSpaces( _XML )
    Function Author: Kevin Lietaer (http://www.fmfunctions.com/mid/602)
    Last updated: 28 January 2019
    Version: 2.0

*/
// ===================================

 

 

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (32)
Date  (28)
XML  (27)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Layout  (15)
Interface  (15)
Text  (14)
Variables  (13)
Filter  (12)
Layout Objects  (11)
Design  (10)
Array  (8)