Jump to content
MMM

Add Attribute

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...