XML an identical copy using XSLT

Discussion in 'XML' started by kluge.wolfram@googlemail.com, May 20, 2008.

  1. Guest

    Hi,

    i get stucked on a transformation problem using XSLT. What i need is
    to copy an XML Tree to an output XML without any automatic changes.
    Since i used <xsl:copy> or <xsl:copy-of> there occur unwanted side
    effects.
    For example i just copied a xml were several namespace declarations
    are present more than one time. Then
    the transformation do remove the declaration at the child nodes.
    Another funny automatism is - if i remove a node
    which holds a namespace declaration the first child is inheriting its
    declaration.

    Thank you for your support,

    Im looking forward to hearing from yoou soon.

    Wolfram
    , May 20, 2008
    #1
    1. Advertising

  2. wrote:

    > i get stucked on a transformation problem using XSLT. What i need is
    > to copy an XML Tree to an output XML without any automatic changes.
    > Since i used <xsl:copy> or <xsl:copy-of> there occur unwanted side
    > effects.
    > For example i just copied a xml were several namespace declarations
    > are present more than one time. Then
    > the transformation do remove the declaration at the child nodes.
    > Another funny automatism is - if i remove a node
    > which holds a namespace declaration the first child is inheriting its
    > declaration.


    If you have e.g.
    <foo xmlns="http://example.com/2008/ns1">
    <bar>
    <baz/>
    </bar>
    </foo>
    then all three elements are in the namespace
    http://example.com/2008/ns1. Consequently if you copy the bar element
    without its foo parent then the serializer has to add a xmlns
    declaration to make sure the copied element is still in its namespace.

    In the XSLT/XPath 1.0 data model there are namespace nodes which are in
    scope for element nodes. And xsl:copy http://www.w3.org/TR/xslt#copying
    copies these namespace nodes.
    With XSLT 2.0 http://www.w3.org/TR/xslt20/#shallow-copy you can specify
    whether namespaces are copied but for the namespace of the element
    itself that would not prevent the copying of its namespace. If you want
    to strip the namespace of an element then you can't use xsl:copy,
    instead you need to create a new element e.g.

    <xsl:template match="pf1:bar"
    xmlns:pf1="http://example.com/2008/ns1">

    <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@* | node()"/>
    </xsl:element>

    </xsl:template>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 20, 2008
    #2
    1. Advertising

  3. Guest

    On 20 Mai, 13:09, Martin Honnen <> wrote:
    > wrote:
    > > i get stucked on a transformation problem using XSLT. What i need is
    > > to copy an XML Tree to an output XML without any automatic changes.
    > > Since i used <xsl:copy> or <xsl:copy-of> there occur unwanted side
    > > effects.
    > > For example i just copied a xml were several namespace declarations
    > > are present more than one time. Then
    > > the transformation do remove the declaration at the child nodes.
    > > Another funny automatism is - if i remove a node
    > > which holds a namespace declaration the first child is inheriting its
    > > declaration.

    >
    > If you have e.g.
    >    <foo xmlns="http://example.com/2008/ns1">
    >      <bar>
    >        <baz/>
    >      </bar>
    >    </foo>
    > then all three elements are in the namespacehttp://example.com/2008/ns1. Consequently if you copy the bar element
    > without its foo parent then the serializer has to add a xmlns
    > declaration to make sure the copied element is still in its namespace.
    >
    > In the XSLT/XPath 1.0 data model there are namespace nodes which are in
    > scope for element nodes. And xsl:copyhttp://www.w3.org/TR/xslt#copying
    > copies these namespace nodes.
    > With XSLT 2.0http://www.w3.org/TR/xslt20/#shallow-copyyou can specify
    > whether namespaces are copied but for the namespace of the element
    > itself that would not prevent the copying of its namespace. If you want
    > to strip the namespace of an element then you can't use xsl:copy,
    > instead you need to create a new element e.g.
    >
    >    <xsl:template match="pf1:bar"
    >      xmlns:pf1="http://example.com/2008/ns1">
    >
    >      <xsl:element name="{local-name()}">
    >        <xsl:apply-templates select="@* | node()"/>
    >      </xsl:element>
    >
    >    </xsl:template>
    > --
    >
    >         Martin Honnen
    >        http://JavaScript.FAQTs.com/


    What i want is the following,

    <ds:foo xmlns:ds="http://example.com/2008/ns1">
    <ds:bar>
    <ds:baz/>
    </ds:bar>
    </ds:foo>

    if i copy the node list ds:bar and ignore <ds:foo> then ds:bar gets
    the declaration of foo.

    <ds:bar xmlns:ds="http://example.com/2008/ns1">
    <ds:baz/>
    </ds:bar>

    this is unwanted and i would like to omit this.

    second behavior is ....

    <ds:foo xmlns:ds="http://example.com/2008/ns1">
    <ds:bar xmlns:ds="http://example.com/2008/ns1>
    <ds:baz/>
    </ds:bar>
    </ds:foo>

    and i copy the hole structure the result looks like shown below

    <ds:foo xmlns:ds="http://example.com/2008/ns1">
    <ds:bar>
    <ds:baz/>
    </ds:bar>
    </ds:foo>

    but this arent the exact copies of there sources.

    Thank You for Help

    Wolfram
    , May 20, 2008
    #3
  4. wrote:

    > What i want is the following,
    >
    > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    > <ds:bar>
    > <ds:baz/>
    > </ds:bar>
    > </ds:foo>
    >
    > if i copy the node list ds:bar and ignore <ds:foo> then ds:bar gets
    > the declaration of foo.
    >
    > <ds:bar xmlns:ds="http://example.com/2008/ns1">
    > <ds:baz/>
    > </ds:bar>
    >
    > this is unwanted and i would like to omit this.


    What exactly do you want to omit? As said, if you want to strip the
    namespace of an element node then use
    <xsl:template match="ds:*"
    xmlns:ds="http://example.com/2008/ns1">

    <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@* | node()"/>
    </xsl:element>

    </xsl:template>


    > but this arent the exact copies of there sources.


    XSLT does not work with the source code, it works on the XSLT/XPath data
    model.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 20, 2008
    #4
  5. Ken Starks Guest

    What happens if you try the following? which is actually
    equivalent as far as the __expanded__ namespaces are
    concerned:


    <ds:foo xmlns:ds="http://example.com/2008/ns1">
    <xyz:bar xmlns:xyz="http://example.com/2008/ns1>
    <ds:baz/>
    </xyz:bar>
    </ds:foo>



    wrote:

    >
    > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    > <ds:bar xmlns:ds="http://example.com/2008/ns1>
    > <ds:baz/>
    > </ds:bar>
    > </ds:foo>
    >
    > and i copy the hole structure the result looks like shown below
    >
    > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    > <ds:bar>
    > <ds:baz/>
    > </ds:bar>
    > </ds:foo>
    >
    > but this arent the exact copies of there sources.
    >
    > Thank You for Help
    >
    > Wolfram
    >
    >
    Ken Starks, May 20, 2008
    #5
  6. Guest

    On 20 Mai, 14:19, Ken Starks <> wrote:
    > What happens if you try the following? which is actually
    > equivalent as far as the __expanded__ namespaces are
    > concerned:
    >
    > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    >   <xyz:bar xmlns:xyz="http://example.com/2008/ns1>
    >         <ds:baz/>
    >   </xyz:bar>
    > </ds:foo>
    >
    >
    >
    > wrote:
    >
    > > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    > >    <ds:bar xmlns:ds="http://example.com/2008/ns1>
    > >       <ds:baz/>
    > >    </ds:bar>
    > > </ds:foo>

    >
    > > and i copy the hole structure the result looks like shown below

    >
    > > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    > >    <ds:bar>
    > >       <ds:baz/>
    > >    </ds:bar>
    > > </ds:foo>

    >
    > > but this arent the exact copies of there sources.

    >
    > > Thank You for Help

    >
    > > Wolfram- Zitierten Text ausblenden -

    >
    > - Zitierten Text anzeigen -


    Hi Ken

    this works

    > <ds:foo xmlns:ds="http://example.com/2008/ns1">
    > <xyz:bar xmlns:xyz="http://example.com/2008/ns1>
    > <ds:baz/>
    > </xyz:bar>
    > </ds:foo>


    i think the reason why it works it the new namspace prefix.
    But what is going on if redundant namespace declarations occurs.

    Thanks

    Wolfram
    , May 20, 2008
    #6
  7. Ken Starks Guest


    >
    > Hi Ken
    >
    > this works
    >
    >> <ds:foo xmlns:ds="http://example.com/2008/ns1">
    >> <xyz:bar xmlns:xyz="http://example.com/2008/ns1>
    >> <ds:baz/>
    >> </xyz:bar>
    >> </ds:foo>

    >
    > i think the reason why it works it the new namspace prefix.
    > But what is going on if redundant namespace declarations occurs.
    >
    > Thanks
    >
    > Wolfram


    I believe the xslt processor is allowed to do its own
    thing, in the case of redundant namespaces, but
    I can't say I've read the specifications and seen
    it in black and white.
    Ken Starks, May 20, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,200
  2. Replies:
    26
    Views:
    2,083
    Roland Pibinger
    Sep 1, 2006
  3. Jason Cavett

    Identical Item in Cut/Copy/Paste

    Jason Cavett, Apr 18, 2007, in forum: Java
    Replies:
    2
    Views:
    443
    Jason Cavett
    Apr 20, 2007
  4. Replies:
    2
    Views:
    334
  5. tlang
    Replies:
    4
    Views:
    105
    Diego Vilar
    Mar 13, 2005
Loading...

Share This Page