Jump to content

Add Attribute


MMM
 Share

Recommended Posts

Hi, I wanna add a little function to my XSLT File. I want to add an attribute with a constant value to an element if it is in a specific depth. My XSLT File merges 2 XML Files so far and looks like this:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="CAEXFile"> <xsl:copy>   <xsl:apply-templates select="@* | node() | document('extra.xml')/CAEXFile/SystemUnitClassLib" /></xsl:copy></xsl:template> <xsl:template match="node() | @*"><xsl:copy-of select="." /></xsl:template> </xsl:stylesheet>

Then I want to add the attribute:

<xsl:template match="CAEXFile/InstanceHierarchy/InternalElement/InternalElement">  <xsl:attribute name="FurtherInformation">ConstantValue</xsl:attribute></xsl:template>

But I'm confused where to put the code, because theres already a match "CAEXFile" to copy the whole tree. I'd be very glad if someone could help me with that.

Link to comment
Share on other sites

First, adjust your copy-of template to only do this for attributes and text nodes, like:

<xsl:template match="text() | @*"><xsl:copy-of select="." /></xsl:template>

Then create a new template that copies elements, but also applies inner templates, like:

<xsl:template match="*"><xsl:copy>   <xsl:apply-templates/></xsl:copy></xsl:template>

Once you do that, you should be able to have the template:

<xsl:template match="CAEXFile/InstanceHierarchy/InternalElement/InternalElement">  <xsl:attribute name="FurtherInformation">ConstantValue</xsl:attribute></xsl:template>

and have it work too.

Link to comment
Share on other sites

Thanks for your help. Sadly I can't get it working. 1. If I change the copy-of match from node() to text(), he stops copying all attributes.2. If I add the new template for adding the attribute he stops copying it. So the result is just:

<InstanceHierarchy>        <InternalElement>		 </InternalElement>		 <InternalElement>		 </InternalElement></InstanceHierarchy>

Do I make something wrong? Just say if you need example files to find the issue.

Link to comment
Share on other sites

You need

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

as the base template for all such task, it is the so called "identity transformation" that copies nodes level by level. Now you can add templates for those nodes you want to modify e.g.

<xsl:template match="CAEXFile/InstanceHierarchy/InternalElement/InternalElement">  <xsl:copy>	 <xsl:attribute name="FurtherInformation">ConstantValue</xsl:attribute>	 <xsl:apply-templates select="@* | node()"/>  </xsl:copy></xsl:template>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...