• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • An Introduction to Processing XML with RPG, Part 4

    November 17, 2015 Jon Paris

    Note: The code accompanying this article is available for download here.

    In the first three parts of this series, I focused on the basics of using RPG’s XML-INTO. In this episode I want to wrap things up by covering two of the more recent additions to this support.

    Let’s start with namespaces. I haven’t got time to go into all the whys and wherefores of namespaces. For now let’s just say that they allow you avoid name collisions by qualifying element and attribute names. Since you will encounter them in many documents it is important to understand the options you have in RPG for dealing with them. (You can learn more about namespaces here.)

    The XML extract below shows the simple usage of a namespace.

    <p400:OrderDetail
        p400_OrderNumber="12345"
        p400_Date="2015-11-14"
        >
    <p400:Name>James Smith</p400:Name>
    

    Notice that all the element names are prefixed by the characters “p400:”. This is the shorthand notation for the namespace and it is associated with a URI. In this case that association is made through the highlighted “>As I discussed earlier in the series, in order for XML-INTO to process the XML, the element and attribute names in the document must match the names and hierarchy in the RPG data structure used to receive the data. Teeny tiny problem: The colon (“:”) is not a valid character in an RPG name.

    RPG’s solution is to provide options to either remove the namespace prefix or to convert the colon to a valid RPG name character. This is done via the ns option. Specifying ns=remove as an option to the %XML BIF will simply strip the namespace qualifier completely. ns=merge on the other hand will retain the qualifier and convert the colon to an underscore.

    Below are extracts from the sample programs that demonstrate the two options. The first example shows how the DS needs to be structured when the ns=remove option is specified.

    d OrderDetail...
    d                 DS                  Qualified
    d   OrderNumber                  5a
    d   Date                        10a
    d   Address                           LikeDS(Address_T)
    d                                     Dim(2)
    ...
      xml-into OrderDetail
               %xml( xmlFileName: 'case=any doc=file +
                                      ns=remove');    
    

    This second example shows the changes required when ns=merge is used.

    d p400_OrderDetail...
    d                 DS                  Qualified
    d   p400_OrderNumber...
    d                                5a
    d   p400_Date                   10a
    d   p400_Address                      LikeDS(p400_Address_T)
    d                                     Dim(2)
    ...
      xml-into p400_OrderDetail
               %xml( xmlFileName: 'case=any doc=file +
                                   ns=merge ');
    

    So which option should you choose? In most cases the remove option will be the best choice as it simplifies the variable names, which makes them both easier to type and easier to read. Of course this only works in cases where there is no duplication of names within the document. Luckily that is normally the case. This option also simplifies the processing of standardized XML documents that only differ in the namespace used. But there is a drawback to this.

    Suppose that you receive the same basic document from two or three different suppliers. The remove option allows you to use the same data structures etc. to process the document, but what if you need to vary the processing based on the source of the document? Luckily IBM thought of this and supplied us with an option to capture the value of the namespace that is being stripped off. You do this by specifying the nsprefix option. This operates in a very similar fashion to the countprefix option, which we covered in part 2 in that if we specify nsprefix=ns_ then by adding a field to the receiver DS with the appropriate name we can capture any namespace of our choosing.

    In the example below I have modified the %XML BIF to include the nsprefix option and added the variable ns_OrderNumber in order to capture the prefix associated with the OrderNumber element. Notice that as with the count prefix option the variable to receive the prefix must appear at the same level in the hierarchy as the element whose prefix is to be captured.

    d OrderDetail...
    d                 DS                  Qualified
    d   ns_OrderNumber...
    d                                4a
    d   OrderNumber                  5a
    
      xml-into OrderDetail
               %xml( xmlFileName: 'case=any doc=file
                                   ns=remove nsprefix=ns_');
    

    Before I wrap this up . . .

    There is one more processing option that we haven’t discussed that you will find you need quite often. Just as the colon is not valid in an RPG name, nor are a number of other characters that are valid in XML names. In North America, and most English speaking countries, the most common one that you will encounter is the hyphen. While names including a hyphen are valid in COBOL (for example) they are not valid in RPG. RPG deals with this by allowing us to specify an extension to the case processing option which will cause any hyphens to be converted to underscores. So with case=convert specified, an element name such as Street-Address would be converted to Street_Address as an RPG name.

    Outside of the English speaking world it is also possible to have XML element names that include accented and other characters that are not valid in RPG names. For example characters such as é, ö, and á. With the convert option in use, these would all be converted to their upper case equivalents. i.e., E, O and A. But in some cases there is no simple upper case equivalent, for example the “double” characters æ and œ. In these cases the characters are replaced by underscores.

    It may occur to you at this point, that by the time all these conversions are completed, the resulting name may contain multiple consecutive underscores. That would make them very hard to type accurately since they would tend to appear on the screen as a single long line. RPG deals with this by converting all consecutive underscores to a single one. Thus A___B__C would become A_B_C. In theory this could result in duplicate names but it is highly unlikely to occur.

    There are a few other XML-INTO options that you may have need of from time to time, but hopefully I have covered all of the ones you are likely to encounter in your daily work. As before I have supplied both fixed and free-form examples of the sample programs from which the extracts above were taken. If you download them you will find that I also used the count prefix option in addition to the ns options described here. So that you can “play” with the code I have also included the XML used in the examples. You can download the zip file here here.

    If you have any questions relating to processing XML with RPG, please let me know and we’ll address them in future tips.

    Jon Paris is one of the world’s most knowledgeable experts on programming on the System i platform. Paris cut his teeth on the System/38 way back when, and in 1987 he joined IBM’s Toronto software lab to work on the COBOL compilers for the System/38 and System/36. He also worked on the creation of the COBOL/400 compilers for the original AS/400s back in 1988, and was one of the key developers behind RPG IV and the CODE/400 development tool. In 1998, he left IBM to start his own education and training firm, a job he does to this day with his wife, Susan Gantner–also an expert in System i programming. Paris and Gantner, along with Paul Tuohy and Skip Marchesani, are co-founders of System i Developer, which hosts the new RPG & DB2 Summit conference. Send your questions or comments for Jon to Ted Holt via the IT Jungle Contact page.

    RELATED STORIES

    The Path To XML-INTO Happiness, Part 3

    An Introduction to Processing XML With RPG, Part 2

    An Introduction To Processing XML With RPG, Part 1: The Basics

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    NGS:  Nov. 18 FREE Webinar: Migration Alternatives for Query/400 Users
    United Computer Group:  VAULT400 BaaS delivers secure cloud backup and DR solutions
    BCD:  IBM i Webinar with Jon Paris - RPG OA: So Misunderstood! November 18 at 1pm EST

    HelpSystems’ Application Integration Begins With GUI ERP Upgrades: Love ‘Em or Leave ‘Em?

    Leave a Reply Cancel reply

Volume 15, Number 24 -- November 17, 2015
THIS ISSUE SPONSORED BY:

ProData Computer Services
Connectria
WorksRight Software

Table of Contents

  • An Introduction to Processing XML with RPG, Part 4
  • Formatting Dates with SQL, Take 3
  • Data Scrubbing Functions In DB2 For i

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle