Skip Menu |

This queue is for tickets about the XML-Twig CPAN distribution.

Report information
The Basics
Id: 78813
Status: open
Priority: 0/
Queue: XML-Twig

People
Owner: Nobody in particular
Requestors: roe.mcburnett [...] ericsson.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: map_xmlns bug
Date: Mon, 6 Aug 2012 15:58:33 +0000
To: "'bug-XML-Twig [...] rt.cpan.org'" <bug-XML-Twig [...] rt.cpan.org>
From: "McBurnett, Roe D" <roe.mcburnett [...] ericsson.com>
When using namespaces, it is allowed to identify an element as a particular type in an already defined namespace. the map_xmlns does not convert the namespace prefix for the types correctly. The code: #! /usr/bin/perl use XML::Twig; $x = XML::Twig->new(map_xmlns => {"http://namespace/CommandService" => "cmdsvc", "http://namespace/ShelfService" => "shlsvc" }, pretty_print => "indented"); $x->parse('<ns1:list xmlns:ns1="http://namespace/CommandService" xmlns:ns2="http://namespace/ShelfService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Show quoted text
>
<commands> <commandId>1</commandId> <command xsi:type="ns2:find" > <equipmentFilter>...</equipmentFilter> </command> </commands> <commands> <commandId>2</commandId> <command xsi:type="ns2:getByName" > <name>...</name> </command> </commands> </ns1:list>'); $x->print(); Generates the following: <cmdsvc:list xmlns:cmdsvc="http://namespace/CommandService" xmlns:shlsvc="http://namespace/ShelfService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <commands> <commandId>1</commandId> <command xsi:type="ns2:find"> <equipmentFilter>...</equipmentFilter> </command> </commands> <commands> <commandId>2</commandId> <command xsi:type="ns2:getByName"> <name>...</name> </command> </commands> </cmdsvc:list> It should generate this: <cmdsvc:list xmlns:cmdsvc="http://namespace/CommandService" xmlns:shlsvc="http://namespace/ShelfService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Show quoted text
>
<commands> <commandId>1</commandId> <command xsi:type="shlsvc:find" > <equipmentFilter>...</equipmentFilter> </command> </commands> <commands> <commandId>2</commandId> <command xsi:type="shlsvc:getByName" > <name>...</name> </command> </commands> </cmdsvc:list> This example was created using perl: $ perl -v This is perl, v5.10.1 (*) built for i686-cygwin-thread-multi-64int (with 13 registered patches, see perl -V for more detail) Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. and XML::Twig: Show quoted text
cpan> i XML::Twig
CPAN: Storable loaded ok (v2.30) Reading '/home/cpan/Metadata' Database was generated on Thu, 26 Jul 2012 15:58:03 GMT CPAN: YAML loaded ok (v0.71) Reading 54 yaml files from /home/cpan/build/ CPAN: Time::HiRes loaded ok (v1.9719) ..................................DONE Restored the state of none (in 0.8281 secs) Module id = XML::Twig DESCRIPTION A module for easy processing of XML CPAN_USERID MIROD (Michel Rodriguez <xmltwig@gmail.com>) CPAN_VERSION 3.40 CPAN_FILE M/MI/MIROD/XML-Twig-3.40.tar.gz UPLOAD_DATE 2012-05-10 DSLIP_STATUS RmpOp (released,mailing-list,perl,object-oriented,Standard-Perl) MANPAGE XML::Twig - A perl module for processing huge XML documents in tree mode. INST_FILE /usr/lib/perl5/site_perl/5.10/XML/Twig.pm INST_VERSION 3.40 Show quoted text
cpan>
-Roe McBurnett

Message body is not shown because it is too large.

Subject: Re: [rt.cpan.org #78813] map_xmlns bug
Date: Mon, 06 Aug 2012 18:26:44 +0200
To: bug-XML-Twig [...] rt.cpan.org
From: mirod <xmltwig [...] gmail.com>
On 08/06/2012 05:58 PM, McBurnett, Roe D via RT wrote: Show quoted text
> When using namespaces, it is allowed to identify an element as a > particular type in an already defined namespace. the map_xmlns does > not convert the namespace prefix for the types correctly.
Originally that's supposed to be a feature. The original prefix is restored when outputting the document. Obviously it doesn't work though, since the ns* prefixes are not defined any more in the output. Drats! I'll have a look at this tomorrow. Thanks for the report. -- mirod
Subject: Re: [rt.cpan.org #78813] map_xmlns bug
Date: Mon, 06 Aug 2012 18:49:54 +0200
To: bug-XML-Twig [...] rt.cpan.org
From: mirod <xmltwig [...] gmail.com>
On 08/06/2012 05:58 PM, McBurnett, Roe D via RT wrote: Show quoted text
> When using namespaces, it is allowed to identify an element as a > particular type in an already defined namespace. the map_xmlns does > not convert the namespace prefix for the types correctly.
Actually that's pretty strange, because the behaviour you want is what he module should do. There is even a test, test 640 in t/test_additional.t that checks just this. I am trying to see how this test is different from your example, but so far I can't really see it. -- mirod
Subject: Re: [rt.cpan.org #78813] map_xmlns bug
Date: Mon, 06 Aug 2012 19:07:32 +0200
To: bug-XML-Twig [...] rt.cpan.org
From: mirod <xmltwig [...] gmail.com>
On 08/06/2012 06:50 PM, xmltwig@gmail.com via RT wrote: Show quoted text
> Queue: XML-Twig > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=78813 > > > On 08/06/2012 05:58 PM, McBurnett, Roe D via RT wrote: >
>> When using namespaces, it is allowed to identify an element as a >> particular type in an already defined namespace. the map_xmlns does >> not convert the namespace prefix for the types correctly.
> > > Actually that's pretty strange, because the behaviour you want is what > he module should do. There is even a test, test 640 in > t/test_additional.t that checks just this. I am trying to see how this > test is different from your example, but so far I can't really see it.
Duh! The difference is that you want to replace xsi:type="ns2:find" by xsi:type="shlsvc:find". That's the attribute _value_ that you want to replace. In XML there is nothing special about it, namespaces are not involved. So it won't be replaced. Is this something that W3c schemas-aware processors are supposd to know about? -- mirod
Subject: RE: [rt.cpan.org #78813] map_xmlns bug
Date: Mon, 6 Aug 2012 17:32:26 +0000
To: "'bug-XML-Twig [...] rt.cpan.org'" <bug-XML-Twig [...] rt.cpan.org>
From: "McBurnett, Roe D" <roe.mcburnett [...] ericsson.com>
I did a lookup in the W3 XML Schema definitions and found 2 sections which seem to imply that YES W3c XML Schema-aware processors should know about the imbedded type information within such an attribute. See http://www.w3.org/TR/xmlschema-1/ section 2.6.1 and http://www.w3.org/TR/xmlschema-1/#src-qname for details Of course I am not any knid of XML Schema expert and I could have interpreted these entries incorrectly, but the XML I am using is being used by a Java program using Jaxb version 2 to interpret the XML and it definitely NEEDs the prefix in the attribute VALUE to match the prefix of the namespace that is defined earlier in the XML document. So if map_xmlns changes the prefix on the earlier definition, the value inside the xsi:type attribute needs to be modified as well. Thanks for the quick response to my problem.... XML::Twig is awesome!! -Roe Show quoted text
-----Original Message----- From: xmltwig@gmail.com via RT [mailto:bug-XML-Twig@rt.cpan.org] Sent: Monday, August 06, 2012 1:08 PM To: McBurnett, Roe D Subject: Re: [rt.cpan.org #78813] map_xmlns bug <URL: https://rt.cpan.org/Ticket/Display.html?id=78813 > On 08/06/2012 06:50 PM, xmltwig@gmail.com via RT wrote:
> Queue: XML-Twig > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=78813 > > > On 08/06/2012 05:58 PM, McBurnett, Roe D via RT wrote: >
>> When using namespaces, it is allowed to identify an element as a >> particular type in an already defined namespace. the map_xmlns does >> not convert the namespace prefix for the types correctly.
> > > Actually that's pretty strange, because the behaviour you want is what > he module should do. There is even a test, test 640 in > t/test_additional.t that checks just this. I am trying to see how this > test is different from your example, but so far I can't really see it.
Duh! The difference is that you want to replace xsi:type="ns2:find" by xsi:type="shlsvc:find". That's the attribute _value_ that you want to replace. In XML there is nothing special about it, namespaces are not involved. So it won't be replaced. Is this something that W3c schemas-aware processors are supposd to know about? -- mirod
Subject: Re: [rt.cpan.org #78813] map_xmlns bug
Date: Mon, 06 Aug 2012 21:42:38 +0200
To: bug-XML-Twig [...] rt.cpan.org
From: mirod <xmltwig [...] gmail.com>
On 08/06/2012 07:32 PM, McBurnett, Roe D via RT wrote: Show quoted text
> I did a lookup in the W3 XML Schema definitions and found 2 sections > which seem to imply that YES W3c XML Schema-aware processors should > know about the imbedded type information within such an attribute. > > See http://www.w3.org/TR/xmlschema-1/ section 2.6.1 and > http://www.w3.org/TR/xmlschema-1/#src-qname for details > > Of course I am not any knid of XML Schema expert and I could have > interpreted these entries incorrectly, but the XML I am using is > being used by a Java program using Jaxb version 2 to interpret the > XML and it definitely NEEDs the prefix in the attribute VALUE to > match the prefix of the namespace that is defined earlier in the XML > document. So if map_xmlns changes the prefix on the earlier > definition, the value inside the xsi:type attribute needs to be > modified as well.
I think for now I won't integrate this into the module. I have to think about it some more. In the mean time, below is a way to do this. Note that it relies on the internal structure of the XML::Twig object (the $t->{twig_parser}->{Prefix_Table} thingie). I'll see how I can make this available in the API. #! /usr/bin/perl use strict; use warnings; use Test::More tests => 1; use XML::Twig; my $doc='<ns1:list xmlns:ns1="http://namespace/CommandService" xmlns:ns2="http://namespace/ShelfService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <commands> <commandId>1</commandId> <command xsi:type="ns2:find"> <equipmentFilter>...</equipmentFilter> </command> </commands> <commands> <commandId>2</commandId> <command xsi:type="ns2:getByName"> <name>...</name> </command> </commands> </ns1:list> '; my $expected= $doc; $expected=~ s{ns1}{cmdsvc}g; $expected=~ s{ns2}{shlsvc}g; my %map= reverse (cmdsvc => "http://namespace/CommandService", shlsvc => "http://namespace/ShelfService", xsi => "http://www.w3.org/2001/XMLSchema-instance", ); my $x = XML::Twig->new( map_xmlns => { %map }, twig_handlers => { '*[@xsi:type]' => sub { upd_xsi_type( @_, \%map) } }, pretty_print => "indented" ); $x->parse($doc); is( $x->sprint, $expected, 'xmlns'); sub upd_xsi_type { my( $t, $elt, $map)= @_; my $type= $elt->att( 'xsi:type'); my( $old_prefix)= $type=~ m{^([^:]*):}; my $prefix_table= $t->{twig_parser}->{Prefix_Table}; if( my $new_prefix= $map->{$prefix_table->{$old_prefix}->[0]}) { $type=~ s{^$old_prefix}{$new_prefix}; $elt->set_att( 'xsi:type' => $type); } return 1; }
Subject: RE: [rt.cpan.org #78813] map_xmlns bug
Date: Mon, 6 Aug 2012 20:02:39 +0000
To: "'bug-XML-Twig [...] rt.cpan.org'" <bug-XML-Twig [...] rt.cpan.org>
From: "McBurnett, Roe D" <roe.mcburnett [...] ericsson.com>
I implemented your alternate method and it works a treat!! I will continue to use the workaround until I hear different. Let me know if you're thinking goes in a different direction. You are GREAT! Thanks so much!, -Roe Show quoted text
-----Original Message----- From: xmltwig@gmail.com via RT [mailto:bug-XML-Twig@rt.cpan.org] Sent: Monday, August 06, 2012 3:43 PM To: McBurnett, Roe D Subject: Re: [rt.cpan.org #78813] map_xmlns bug <URL: https://rt.cpan.org/Ticket/Display.html?id=78813 > I think for now I won't integrate this into the module. I have to think about it some more. In the mean time, below is a way to do this. Note that it relies on the internal structure of the XML::Twig object (the $t->{twig_parser}->{Prefix_Table} thingie). I'll see how I can make this available in the API.