my dog learned polymorphism
The moose likes XML and Related Technologies and the fly likes How to remove an attribute from a specific 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 "How to remove an attribute from a specific Element" Watch "How to remove an attribute from a specific Element" New topic

How to remove an attribute from a specific Element

Arpit Mehta

Joined: Oct 06, 2010
Posts: 1
This is what is getting generated automatically using one process:

<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_6e0b628891c999536881cb8dc622ae5c" IssueInstant="2010-05-27T20:40:09.768Z" Version="2.0">
<saml2:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" >YOUR ISSUER</saml2:Issuer>
<saml2:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">_b0d2954c37e201e7749fc7bbffde1861</saml2:NameID>
<saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
<saml2:SubjectConfirmationData NotBefore="2010-05-27T20:40:09.768Z" NotOnOrAfter="2010-05-27T20:45:09.768Z" />
<saml2:Conditions NotBefore="2010-05-27T20:40:09.768Z" NotOnOrAfter="2010-05-27T20:45:09.768Z"> <saml2:AudienceRestriction>
<saml2:AuthnStatement AuthnInstant="2010-05-27T20:40:09.616Z" SessionIndex="e00aafb5ce47bdc443e6562fdcb77feb9deb1e32fe98dbea3b01e307c9ef50b3">
<saml2:SubjectLocality Address=""/>
<saml2:AuthnContext> <saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml2:AuthnContextClassRef>

We need to remove @NotBefore attribute from saml2:SubjectConfirmationData element and add a new attribute @Recipient. I have the code to add new attribute to specific element but not able to remove attribute from that element

To add new attribute in a specific Element

<xsl:template match="@*|node()">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates mode="add-atts" select="."/>

<!-- By default, don't add any attributes -->
<xsl:template mode="add-atts" match="*"/>

<xsl:template mode="add-atts" match="*[namespace-uri()='urn:oasis:names:tc:SAML:2.0:assertion' and local-name()='SubjectConfirmationData']">
<xsl:attribute name="Recipient"></xsl:attribute>

Mainly we are looking for a code to remove an attribute from a specific Element.
Note : @NotBefore attribute is also present in saml2:Conditions element which should not be removed or changed

I have used a code to remove attribute but it is removing from all the elements where ever it is finding that attribute but i need to remove is from only one element.

To remove the @NotBefore attribute

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

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

<xsl:template match="@NotBefore"/>
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Right now you have a template which simply ignores all instances of an attribute, which ensures that no instances of the attribute are written to the output. If you want a template that copies only some instances of an attribute, then you'll have to write that template.

It appears you want to write all instances of that attribute except if the parent of the attribute is... something? Then write that code. I'm sure you already know how to do that, I suspect your confusion is how to code the "... something" part. Am I right? If so then you'll have to describe more clearly what that part is.
I agree. Here's the link:
subject: How to remove an attribute from a specific Element
jQuery in Action, 3rd edition