You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by ajay bhadauria <ab...@yahoo.com> on 2011/11/07 19:32:35 UTC

Need to copy all elements of a parent element using xalan c++ (1.10)

Hi,
 
I need to copy all the child elements from the parent element to output xml using xalan c++  transform method. However, one of the child element is prefixed but does not have corresponding namespace. 
 
What should I do in xslt so that all the children elements of parent element are copied to output xml file without caring the namespace. Altova XMLSpy does that but When I use xalan c++ 1.10 , it does not copy.  
 
How I can achieve this using xalan c++  1.10 ?
 
E.g.
Input XML file
============
<?xml version="1.0" encoding="UTF-8"?>
<SwInt:Request >
                <SwInt:RequestHeader>
                        <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
                        <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
                        <SwInt:RequestRef>xyz</SwInt:RequestRef>
                </SwInt:RequestHeader>
</SwInt:Request>
XSLT file
======
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
   xmlns:Sw="urn:swift:snl:ns.Sw" xmlns:SwInt="urn:swift:snl:ns.SwInt" xmlns:SwSec="urn:swift:snl:ns.SwSec" >
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:template match="/">
                <xsl:copy-of select="/SwInt:Request/SwInt:RequestHeader"/>
        </xsl:template>
</xsl:stylesheet>
 
Output Xml comes with 
<?xml version="1.0" encoding="UTF-8"?>
 
Regards
Ajay 

Re: Need to copy all elements of a parent element using xalan c++ (1.10)

Posted by sh...@e-z.net.
> On 11/7/2011 10:32 AM, ajay bhadauria wrote:
>> Hi,
>>
>> I need to copy all the child elements from the parent element to output
>> xml using xalan c++ transform method. However, one of the child element
>> is prefixed but does not have corresponding namespace.
>>
>> What should I do in xslt so that all the children elements of parent
>> element are copied to output xml file without caring the namespace.
>> Altova XMLSpy does that but When I use xalan c++ 1.10 , it does not
>> copy.
>>
>> How I can achieve this using xalan c++ 1.10 ?
>>
>> E.g.
>> Input XML file
>> ============
>> <?xml version="1.0" encoding="UTF-8"?>
>> <SwInt:Request >
>> <SwInt:RequestHeader>
>> <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>> <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>> <SwInt:RequestRef>xyz</SwInt:RequestRef>
>> </SwInt:RequestHeader>
>> </SwInt:Request>
> This document does not conform to the namespaces recommendation, so you
> cannot transform it using an XSLT processor.
>
> I have no idea what behavior XML Spy implements, but the XSLT
> recommendation is clear that the document must conform to the namespaces
> recommendation:
>
> http://www.w3.org/TR/xslt#section-Introduction
>
> Is this really the exact document you're using, because I don't see how
> Xerces-C would even parse it correctly, much less get any result from
> Xalan-C.
>
> Dave
>
>

A possibility - but I have not tested - is to coerce ParseSource
to create an instance of XSLTInputSource to use for the XML document
the Xerces-C  WFXMLReader instead of the default IGXMLReader.
The WFXMLReader looks only for basic XML well-formedness and does not
do any namespace prefix mapping or namespace associated attribute
processing.  The IGXMLReader by default knows about namespaces
and QNames.

Your failure is in the use of the Xerces-C IGXMLReader.

The XALAN project, as designed, conforms to the documented standards
and therefore uses the Xerces-C IGXMLReader.

Steven J. Hathaway



Re: Need to copy all elements of a parent element using xalan c++ (1.10)

Posted by David Bertoni <db...@apache.org>.
On 11/7/2011 10:32 AM, ajay bhadauria wrote:
> Hi,
>
> I need to copy all the child elements from the parent element to output
> xml using xalan c++ transform method. However, one of the child element
> is prefixed but does not have corresponding namespace.
>
> What should I do in xslt so that all the children elements of parent
> element are copied to output xml file without caring the namespace.
> Altova XMLSpy does that but When I use xalan c++ 1.10 , it does not copy.
>
> How I can achieve this using xalan c++ 1.10 ?
>
> E.g.
> Input XML file
> ============
> <?xml version="1.0" encoding="UTF-8"?>
> <SwInt:Request >
> <SwInt:RequestHeader>
> <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
> <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
> <SwInt:RequestRef>xyz</SwInt:RequestRef>
> </SwInt:RequestHeader>
> </SwInt:Request>
This document does not conform to the namespaces recommendation, so you 
cannot transform it using an XSLT processor.

I have no idea what behavior XML Spy implements, but the XSLT 
recommendation is clear that the document must conform to the namespaces 
recommendation:

http://www.w3.org/TR/xslt#section-Introduction

Is this really the exact document you're using, because I don't see how 
Xerces-C would even parse it correctly, much less get any result from 
Xalan-C.

Dave

Re: Need to copy all elements of a parent element using xalan c++ (1.10)

Posted by sh...@e-z.net.
If you can get a handle to specify properties for the underlying
Xerces XMLReaders, you might try setting the UsingNamespaces to false.
The default for Xalan when creating XMLReaders is "true".

- Steve

>
> The issue may be in the Xerces-C XMLReader when trying to construct a
> nodeset.
> I have not tried such LAX parsing of XML documents with Xerces-C
> callbacks.
>
> When namespace prefixes are used, indicating qualified names, the
> namespaces must be specified.  This is part of wellformedness.  The
> presence of prefixes without qualifying names will cause the nodeset
> generators to fail during namespace resolution, resulting in an empty
> nodeset.
>
> I have tried several methods trying to workaround your absence of
> namespace qualified prefixes and failed.  If you must have this
> capability,
> you may need to coerce your source into a nodeset by other means and
> supply your own document tree walker. - Good Luck!
>
> Maybe someone else can shed some light on how to set specific features
> for XMLReaders to accomplish what you want.
>
> - Steve
>
> On 11/7/2011 5:39 PM, shathawa@e-z.net wrote:
>>> Hi,
>>>
>>> I need to copy all the child elements from the parent element to output
>>> xml using xalan c++  transform method. However, one of the child
>>> element
>>> is prefixed but does not have corresponding namespace.
>>>
>>> What should I do in xslt so that all the children elements of parent
>>> element are copied to output xml file without caring the namespace.
>>> Altova
>>> XMLSpy does that but When I use xalan c++ 1.10 , it does not copy.
>>>
>>> How I can achieve this using xalan c++  1.10 ?
>>>
>>> E.g.
>>> Input XML file
>>> ============
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <SwInt:Request>
>>>                  <SwInt:RequestHeader>
>>>
>>> <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>>>
>>> <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>>>                          <SwInt:RequestRef>xyz</SwInt:RequestRef>
>>>                  </SwInt:RequestHeader>
>>> </SwInt:Request>
>>> XSLT file
>>> ======
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <xsl:stylesheet version="1.0"
>>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>>>     xmlns:Sw="urn:swift:snl:ns.Sw" xmlns:SwInt="urn:swift:snl:ns.SwInt"
>>> xmlns:SwSec="urn:swift:snl:ns.SwSec">
>>>          <xsl:output method="xml" version="1.0" encoding="UTF-8"
>>> indent="yes"/>
>>>          <xsl:template match="/">
>>>                  <xsl:copy-of
>>> select="/SwInt:Request/SwInt:RequestHeader"/>
>>>          </xsl:template>
>>> </xsl:stylesheet>
>>>
>>> Output Xml comes with
>>> <?xml version="1.0" encoding="UTF-8"?>
>>>
>>> Regards
>>> Ajay
>> All namespace prefixes in your XML file should be namespace qualified.
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <SwInt:Request  xmlns:SwInt="urn:swift:snl:ns.SwInt"
>>                  xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
>>   <SwInt:RequestHeader>
>>    <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>>    <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>>    <SwInt:RequestRef>xyz</SwInt:RequestRef>
>>   </SwInt:RequestHeader>
>> </SwInt:Request>
>>
>> The following is an edit of your xslt file
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xsl:stylesheet version="1.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>>      xmlns:SwInt="urn:swift:snl:ns.SwInt"
>>      xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
>>   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
>>       omit-xml-declaration="yes"/>
>>   <xsl:template match="/">
>>       <xsl:copy-of select="/SwInt:Request/SwInt:RequestHeader"/>
>>   </xsl:template>
>> </xsl:stylesheet>
>>
>> Producing the output file
>>
>> <SwInt:RequestHeader xmlns:SwInt="urn:swift:snl:ns.SwInt"
>> xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
>>    <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>>    <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>>    <SwInt:RequestRef>xyz</SwInt:RequestRef>
>>   </SwInt:RequestHeader>
>>
>> I am unclear on what you are actually trying to do:
>>
>> You only need in the stylesheet the prefix declarations used in the
>> stylesheet.  The xml documents need namespace declarations for the
>> prefixes they use.
>>
>> Steven J. Hathaway
>>
>>
>>
>
>



Re: Need to copy all elements of a parent element using xalan c++ (1.10)

Posted by Steve Hathaway <sh...@e-z.net>.
The issue may be in the Xerces-C XMLReader when trying to construct a 
nodeset.
I have not tried such LAX parsing of XML documents with Xerces-C callbacks.

When namespace prefixes are used, indicating qualified names, the
namespaces must be specified.  This is part of wellformedness.  The
presence of prefixes without qualifying names will cause the nodeset
generators to fail during namespace resolution, resulting in an empty 
nodeset.

I have tried several methods trying to workaround your absence of
namespace qualified prefixes and failed.  If you must have this capability,
you may need to coerce your source into a nodeset by other means and
supply your own document tree walker. - Good Luck!

Maybe someone else can shed some light on how to set specific features
for XMLReaders to accomplish what you want.

- Steve

On 11/7/2011 5:39 PM, shathawa@e-z.net wrote:
>> Hi,
>>   
>> I need to copy all the child elements from the parent element to output
>> xml using xalan c++  transform method. However, one of the child element
>> is prefixed but does not have corresponding namespace.
>>   
>> What should I do in xslt so that all the children elements of parent
>> element are copied to output xml file without caring the namespace. Altova
>> XMLSpy does that but When I use xalan c++ 1.10 , it does not copy. 
>>   
>> How I can achieve this using xalan c++  1.10 ?
>>   
>> E.g.
>> Input XML file
>> ============
>> <?xml version="1.0" encoding="UTF-8"?>
>> <SwInt:Request>
>>                  <SwInt:RequestHeader>
>>
>> <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>>
>> <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>>                          <SwInt:RequestRef>xyz</SwInt:RequestRef>
>>                  </SwInt:RequestHeader>
>> </SwInt:Request>
>> XSLT file
>> ======
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xsl:stylesheet version="1.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>>     xmlns:Sw="urn:swift:snl:ns.Sw" xmlns:SwInt="urn:swift:snl:ns.SwInt"
>> xmlns:SwSec="urn:swift:snl:ns.SwSec">
>>          <xsl:output method="xml" version="1.0" encoding="UTF-8"
>> indent="yes"/>
>>          <xsl:template match="/">
>>                  <xsl:copy-of select="/SwInt:Request/SwInt:RequestHeader"/>
>>          </xsl:template>
>> </xsl:stylesheet>
>>   
>> Output Xml comes with
>> <?xml version="1.0" encoding="UTF-8"?>
>>   
>> Regards
>> Ajay
> All namespace prefixes in your XML file should be namespace qualified.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <SwInt:Request  xmlns:SwInt="urn:swift:snl:ns.SwInt"
>                  xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
>   <SwInt:RequestHeader>
>    <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>    <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>    <SwInt:RequestRef>xyz</SwInt:RequestRef>
>   </SwInt:RequestHeader>
> </SwInt:Request>
>
> The following is an edit of your xslt file
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>      xmlns:SwInt="urn:swift:snl:ns.SwInt"
>      xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
>   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
>       omit-xml-declaration="yes"/>
>   <xsl:template match="/">
>       <xsl:copy-of select="/SwInt:Request/SwInt:RequestHeader"/>
>   </xsl:template>
> </xsl:stylesheet>
>
> Producing the output file
>
> <SwInt:RequestHeader xmlns:SwInt="urn:swift:snl:ns.SwInt"
> xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
>    <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>    <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>    <SwInt:RequestRef>xyz</SwInt:RequestRef>
>   </SwInt:RequestHeader>
>
> I am unclear on what you are actually trying to do:
>
> You only need in the stylesheet the prefix declarations used in the
> stylesheet.  The xml documents need namespace declarations for the
> prefixes they use.
>
> Steven J. Hathaway
>
>
>


Re: Need to copy all elements of a parent element using xalan c++ (1.10)

Posted by sh...@e-z.net.
> Hi,
>  
> I need to copy all the child elements from the parent element to output
> xml using xalan c++  transform method. However, one of the child element
> is prefixed but does not have corresponding namespace.
>  
> What should I do in xslt so that all the children elements of parent
> element are copied to output xml file without caring the namespace. Altova
> XMLSpy does that but When I use xalan c++ 1.10 , it does not copy. 
>  
> How I can achieve this using xalan c++  1.10 ?
>  
> E.g.
> Input XML file
> ============
> <?xml version="1.0" encoding="UTF-8"?>
> <SwInt:Request >
>                 <SwInt:RequestHeader>
>                        
> <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
>                        
> <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
>                         <SwInt:RequestRef>xyz</SwInt:RequestRef>
>                 </SwInt:RequestHeader>
> </SwInt:Request>
> XSLT file
> ======
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>    xmlns:Sw="urn:swift:snl:ns.Sw" xmlns:SwInt="urn:swift:snl:ns.SwInt"
> xmlns:SwSec="urn:swift:snl:ns.SwSec" >
>         <xsl:output method="xml" version="1.0" encoding="UTF-8"
> indent="yes"/>
>         <xsl:template match="/">
>                 <xsl:copy-of select="/SwInt:Request/SwInt:RequestHeader"/>
>         </xsl:template>
> </xsl:stylesheet>
>  
> Output Xml comes with
> <?xml version="1.0" encoding="UTF-8"?>
>  
> Regards
> Ajay

All namespace prefixes in your XML file should be namespace qualified.

<?xml version="1.0" encoding="UTF-8"?>
<SwInt:Request  xmlns:SwInt="urn:swift:snl:ns.SwInt"
                xmlns:SwInt1="urn:swift:snl:ns.SwInt1" >
 <SwInt:RequestHeader>
  <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
  <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
  <SwInt:RequestRef>xyz</SwInt:RequestRef>
 </SwInt:RequestHeader>
</SwInt:Request>

The following is an edit of your xslt file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:SwInt="urn:swift:snl:ns.SwInt"
    xmlns:SwInt1="urn:swift:snl:ns.SwInt1" >
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
     omit-xml-declaration="yes"/>
 <xsl:template match="/">
     <xsl:copy-of select="/SwInt:Request/SwInt:RequestHeader"/>
 </xsl:template>
</xsl:stylesheet>

Producing the output file

<SwInt:RequestHeader xmlns:SwInt="urn:swift:snl:ns.SwInt"
xmlns:SwInt1="urn:swift:snl:ns.SwInt1">
  <SwInt1:Requestor>cn=requestor,o=pimxbebb,o=swift1</SwInt1:Requestor>
  <SwInt:Responder>cn=responder,o=pimxus33,o=swift</SwInt:Responder>
  <SwInt:RequestRef>xyz</SwInt:RequestRef>
 </SwInt:RequestHeader>

I am unclear on what you are actually trying to do:

You only need in the stylesheet the prefix declarations used in the
stylesheet.  The xml documents need namespace declarations for the
prefixes they use.

Steven J. Hathaway