aspose file tools*
The moose likes XML and Related Technologies and the fly likes how to copy and modify element's attribute ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to copy and modify element Watch "how to copy and modify element New topic
Author

how to copy and modify element's attribute ?

FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
If I do
<xsl:apply-templates select="@*"/> I can copy all the attribute and then i can modify attribute by using <xsl:attribute....>

Then I can use

<xsl:apply-atemplates select="*"/> to copy all the elements. But then how do i modify their attributes ??

I tried using <xsl:attribute...> it doesn't work because it assumes I can referring to the attributes of root element instead of the current element/node..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Originally posted by FY Hsieh:
Then I can use

<xsl:apply-atemplates select="*"/> to copy all the elements. But then how do i modify their attributes ??
Declare a template that matches one (or all) of those elements. Then you can copy the elements however you want. If inside that you put <xsl:apply-templates select="@*"/> then you can copy the attributes, or you can declare a template that matches an attribute and does something different.

Hard to be more specific with such an un-specific question.
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
Originally posted by Paul Clapham:
Declare a template that matches one (or all) of those elements. Then you can copy the elements however you want. If inside that you put <xsl:apply-templates select="@*"/> then you can copy the attributes, or you can declare a template that matches an attribute and does something different.

Hard to be more specific with such an un-specific question.


First I have a simple copy templet --

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

Then in my XSLT, I have

<xsl:apply-templates select="MyElement">
<xsl:attribute name="MyName"><xsl:value-of select="..."/></xsl:attribute>
</xsl:apply-templates>

I tried to match "MyElement" then I reset the "MyName" attribute of "MyElement" by a new value. This failed as it complained "unexpected child" for <xsl:attribute name='MyName">. I think it considers "MyName as an attribute of the root element instead of "MyElement"'s. How can I reset "MyElement"'s attribute (suppose I have many attributes so I don't want to hard code separate templates, each one mapping to an attribute (too tedious).

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

I don't know what it means to "reset" an attribute. But in general if you want to copy an XML from input to output with minor changes, it's best to start with an identity transformation:Then if, for some particular node, you want to do something different than copying it to the output, you write a template that matches that node and does that different thing. For example if you want to do something different for a MyName attribute of a MyElement element then you do this:where the "..." part is what you actually want to do. If you want to do nothing, i.e. exclude the attribute from the output, then put nothing there. I suspect that an <xsl:attribute> element should work in that context.

It might also help you to know that you can write templates that match many different elements:
[ August 07, 2006: Message edited by: Paul Clapham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to copy and modify element's attribute ?