carriage returns in xml elements

14 views
Skip to first unread message

Brian Nolan

unread,
Aug 24, 2018, 11:38:47 AM8/24/18
to SWI-Prolog
Given the following simple XML file (an XMI model file, attached, TraceabilityTest.uml), I am having issues with the carriage returns embedded in the parsed doc/element that gets returned:

<?xml version="1.0" encoding="UTF-8"?>
<uml:Model xmi:version="20131001" xmlns:xmi="http://www.omg.org/spec/XMI/20131001" xmlns:uml="http://www.eclipse.org/uml2/5.0.0/UML" xmi:id="_kcCPIJEFEeiFAJhXHte-Tg" name="TraceabilityTest">
  <packageImport xmi:type="uml:PackageImport" xmi:id="_kgyzUJEFEeiFAJhXHte-Tg">
    <importedPackage xmi:type="uml:Model" href="pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0"/>
  </packageImport>
  <packagedElement xmi:type="uml:Package" xmi:id="_pRVKIJEFEeiFAJhXHte-Tg" name="PackageA">
    <packagedElement xmi:type="uml:Package" xmi:id="_2_Y1kJEFEeiFAJhXHte-Tg" name="PackageA1">
      <packagedElement xmi:type="uml:Package" xmi:id="_6sFw0JEFEeiFAJhXHte-Tg" name="PackageA1a">
        <packagedElement xmi:type="uml:Class" xmi:id="_-UeMwJEFEeiFAJhXHte-Tg" name="Foo"/>
      </packagedElement>
    </packagedElement>
  </packagedElement>
  <packagedElement xmi:type="uml:Package" xmi:id="_xepw4JEFEeiFAJhXHte-Tg" name="PackageB"/>
  <packagedElement xmi:type="uml:Package" xmi:id="_0etokJEFEeiFAJhXHte-Tg" name="PackageC"/>
</uml:Model>

I read the above with load_structure, and then asserted it as traceTest(Doc):

load_traceTest(Doc):-load_structure('C:..path to TraceabilityTest..\\TraceabilityTest.uml',Doc,[]),assertz(traceTest(Doc)).

Here is the prolog [Doc] that gets returned:

Doc = [element('uml:Model', ['xmi:version'='20131001', 'xmlns:xmi'='http://www.omg.org/spec/XMI/20131001', 'xmlns:uml'='http://www.eclipse.org/uml2/5.0.0/UML', 'xmi:id'='_kcCPIJEFEeiFAJhXHte-Tg', name='TraceabilityTest'], ['\n  ', element(packageImport, ['xmi:type'='uml:PackageImport', 'xmi:id'='_kgyzUJEFEeiFAJhXHte-Tg'], ['\n    ', element(importedPackage, ['xmi:type'='uml:Model', href='pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0'], []), '\n  ']), '\n  ', element(packagedElement, ['xmi:type'='uml:Package', 'xmi:id'='_pRVKIJEFEeiFAJhXHte-Tg', name='PackageA'], ['\n    ', element(packagedElement, ['xmi:type'='uml:Package', 'xmi:id'='_2_Y1kJEFEeiFAJhXHte-Tg', name='PackageA1'], ['\n      ', element(packagedElement, ['xmi:type'='uml:Package', 'xmi:id'='_6sFw0JEFEeiFAJhXHte-Tg', name='PackageA1a'], ['\n        ', element(packagedElement, ['xmi:type'='uml:Class', 'xmi:id'='_-UeMwJEFEeiFAJhXHte-Tg', name='Foo'], []), '\n      ']), '\n    ']), '\n  ']), '\n  ', element(packagedElement, ['xmi:type'='uml:Package', 'xmi:id'='_xepw4JEFEeiFAJhXHte-Tg', name='PackageB'], []), '\n  ', element(packagedElement, ['xmi:type'='uml:Package', 'xmi:id'='_0etokJEFEeiFAJhXHte-Tg', name='PackageC'], []), '\n'])].

How do I get rid of the carriage returns?  I have tried  load_structure('TraceabilityTest,Doc,[space(remove)]  and space(default), but the results seem to be the same.  The carriage returns (\n) make it somewhat difficult to process the elements and their children.

I have created two utility predicates, starts_with_return/1 and remove_whitespace/2 (misnamed for now) as follows:

starts_with_return(Atom) :- atom(Atom),sub_atom(Atom,0,1,_,'\n').

remove_whitespace(L,M):-rem_ws_acc(L,[],M).

rem_ws_acc([],A,A).
rem_ws_acc([H|T],A,L):- starts_with_return(H), rem_ws_acc(T,A,L).
rem_ws_acc([H|T],A,L):- rem_ws_acc(T,[H|A],L).

but I'm not overly happy with how they're working as I try to process the elements--it adds a level of complexity I'd rather do without.

I don't need to write the xml back out, so I don't care about the carriage returns.

Any help will be much appreciated! 
TraceabilityTest.uml

Jan Wielemaker

unread,
Aug 25, 2018, 3:38:10 AM8/25/18
to Brian Nolan, SWI-Prolog

27 ?- load_xml('TraceabilityTest.uml', DOM, [space(remove)]),
print_term(DOM, []).
[ element('uml:Model',
[ 'xmi:version' = '20131001',
'xmlns:xmi' = 'http://www.omg.org/spec/XMI/20131001',
'xmlns:uml' = 'http://www.eclipse.org/uml2/5.0.0/UML',
'xmi:id' = '_kcCPIJEFEeiFAJhXHte-Tg',
name = 'TraceabilityTest'
],
[ element(packageImport,
[ 'xmi:type' = 'uml:PackageImport',
'xmi:id' = '_kgyzUJEFEeiFAJhXHte-Tg'
],
[ element(importedPackage,
[ 'xmi:type' = 'uml:Model',
href = 'pathmap://UML_LIBRARIES/UMLPrimitiveTypes.library.uml#_0'
],
[])
]),
element(packagedElement,
[ 'xmi:type' = 'uml:Package',
'xmi:id' = '_pRVKIJEFEeiFAJhXHte-Tg',
name = 'PackageA'
],
...

I think the load_structure/3 didn't work because the <?xml header in the
document causes the parser to switch to xml mode and set xml defaults.
load_xml/3 is a utility around load_structure/3 that prepares for XML
processing.

Cheers --- Jan
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Brian Nolan

unread,
Aug 27, 2018, 9:37:07 AM8/27/18
to SWI-Prolog
That seems to have done the trick! Thanks!!
Reply all
Reply to author
Forward
0 new messages