aspose file tools*
The moose likes XML and Related Technologies and the fly likes use XSLT to copy attribute/element and generate element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "use XSLT to copy attribute/element and generate element" Watch "use XSLT to copy attribute/element and generate element" New topic
Author

use XSLT to copy attribute/element and generate element

FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
This is my first time using javaranch. Thanks in advance..

My XSLT snippnet --

<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:element name="NEW_ELEMENT"><xsl:attribute name="id"><xsl:value-of select="123"/></xsl:attribute>
</xsl:element>
<xsl:apply-templates select="OLD_EELEMENT"/>
</xsl:copy>


what I did is to a) copy the attributes, create a new element, and copy the original element. That's it. It worked but sometimes in the new XML, within the new element called "NEW_ELEMENT" it appends an attribute like "xmlns:fo="http://www.w3.org/1999/XSL/Format". Why ? How to get rid of it ? Note: I have already defined xmlns:xsl and "xmlns:fo" and in the VERY beginning of the XSLT file.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

If you don't want those namespaces copied to your output, then say so in your <xsl:stylesheet> element by specifying the exclude-result-prefixes attribute.
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
Originally posted by Paul Clapham:
If you don't want those namespaces copied to your output, then say so in your <xsl:stylesheet> element by specifying the exclude-result-prefixes attribute.


Thanks. How and what to set ? In the top of my XML I only have

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xslutput method="xml" omit-xml-declaration="no" indent="yes" cdata-section-elements="CONTENT SUBJECT"/>

I don't know what to set for this "exclude-result-prefixes attribute" ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You can find it here, for example, and in many other XSLT tutorial sites on the web.
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
Originally posted by Paul Clapham:
You can find it here, for example, and in many other XSLT tutorial sites on the web.


I checked this site. It tells us to put a "white space separated list of namespace" for this attribute. However, as I said, I didn't define any name space in my XSLT header (see the snippet i attached in last message). So, what should I put there ??

thanks.
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
Originally posted by Paul Clapham:
You can find it here, for example, and in many other XSLT tutorial sites on the web.


I checked this site. It tells us to put a "white space separated list of namespace" for this attribute. However, as I said, I didn't define any name space in my XSLT header (see the snippet i attached in last message). So, what should I put there ??

thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by FY Hsieh:
However, as I said, I didn't define any name space in my XSLT header (see the snippet i attached in last message).
No, you didn't say any such thing. You said
In the top of my XML I only have

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
So in fact you declared two namespaces there. And you said the same thing in your original post, too.
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
I see. So, since I have
"xmlns:fo="http://www.w3.org/1999/XSL/Format" appended to the new generated xsl:element and I don't want it, I just need to add

<xsl:stylesheet exclude-result-prefix="fo">

right ??

Thanks.
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
well, my case is not "fo" namespace appear in the new "element". It works like this --

In the new XML file, it shows the new element

<NEW_ELEMENT id="123" xmlns:fo="http://www.w3.org/1999/XSL/Format"/>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by FY Hsieh:
well, my case is not "fo" namespace appear in the new "element". It works like this --

In the new XML file, it shows the new element

<NEW_ELEMENT id="123" xmlns:fo="http://www.w3.org/1999/XSL/Format"/>
I'm obviously not understanding your problem. The "fo" namespace is clearly being declared in the new element. So if that isn't your problem, what is?

And if you really don't want the "fo" namespace to be declared there automatically, then you don't putat the top of your stylesheet. You put this:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use XSLT to copy attribute/element and generate element