AMT Help Files
homeLION Language ReferenceSyntaxXML FunctionsConsuming XML with Namespaces

Consuming XML with Namespaces

Consuming an XML which uses Namespaces can be accomplished in AMT by using XMLNamespaceManager. This page shows an example of how to correctly use this function.

For this example we will use the following XML file as input:

<?xml version="1.0"?>
<inv:Invoices xmlns:inv="http://somedomain.com/Invoices/2009/v3.2">
  <FileHeader> 
    <SchemaVersion>3.2</SchemaVersion> 
    <Modality>I</Modality>
    <InvoiceIssuerType>EM</InvoiceIssuerType> 
    <Batch>
      <BatchIdentifier>02016003514 / 02016003514</BatchIdentifier> 
      <InvoicesCount>1</InvoicesCount> 
      <TotalInvoicesAmount>
        <TotalAmount>2581.410000</TotalAmount>
        <EquivalentInEuros>2581.410000</EquivalentInEuros>
      </TotalInvoicesAmount>
      <TotalOutstandingAmount>
        <TotalAmount>2581.410000</TotalAmount>
        <EquivalentInEuros>2581.410000</EquivalentInEuros>
      </TotalOutstandingAmount>
      <TotalExecutableAmount>
        <TotalAmount>2581.410000</TotalAmount>
        <EquivalentInEuros>2581.410000</EquivalentInEuros>
      </TotalExecutableAmount>
      <InvoiceCurrencyCode>EUR</InvoiceCurrencyCode>
    </Batch>
  </FileHeader>
</inv:Invoices>

Code example

begin_definitions

var

    path_to_file     : string
    xpathquery       : string
    xmlfile          : xml
    update_node      : xmlnode
    update_node_list : xmlnodes
    nsmgr            : xmlnamespacemanager

end_definitions

routine main
begin_routine

  path_to_file := 'D:\example.xml'

  xmlfile.load (path_to_file)
  update_node := xmlfile.root
  
  sme (update_node.name)
  xpathquery := '/inv:Invoices/FileHeader/Batch/*'

  // A namespace manager is needed to query a document that uses one or more namespaces.
  // We declared an XmlNamespaceManager object, we must now assign a name table to it
  // and add the namespace(s) used in the document.

  // An XmlNameTable object must be assigned to the namespace manager's NameTable
  // property in order to make the XmlNamespaceManager object functional. This is
  // just how MSXML works, which is aggregated by Lion. We could declare our own
  // XmlNameTable object and use that but it is more effective to share the one
  // owned by the XML object because this will already be filled with all the names
  // used in the loaded document.
  nsmgr.nametable := xmlfile.nametable
  nsmgr.addnamespace ('inv', 'http://somedomain.com/Invoices/2009/v3.2')

  // the namespace object is now ready to be used for XPath queries, note
  // we are passing the namespace manager object as a second argument
  update_node_list := update_node.selectnodes (xpathquery, nsmgr)

  sme (getlastresult)
  loop for each update_node in update_node_list
  sme (update_node.name)
  endloop

end_routine

Contents

 Go to top