Jump to content

erasing attributes from XML with an XSL


drg1022
 Share

Recommended Posts

I have a sample XML here.... what I would like to do is keep the exact same structure, but erase all of the attributes.<?xml version="1.0" encoding="utf-8"?><standard_area content_type_id="1" content_id="1"> <value>Arts</value> <level content_type_id="3" content_id="14"> <value>Alternate</value> <standard content_type_id="6" content_id="32"> <value>1-Creating, Performing and Participating in the Arts</value> <key_idea_title content_type_id="7" content_id="85"> <value>Movement</value> <key_idea content_type_id="8" content_id="145"> <value>happy time harry. </value> <performance_indicator content_type_id="9" content_id="356"> <value> create and perform simple dances. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="364"> <value> identify and demonstrate movement elements and skills (such as bend, twist, slide, skip, hop, walk in a straight line). </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="366"> <value> interpret words into a dance. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="369"> <value> participate in movement activities. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="370"> <value> perform individually or in a group. </value> </performance_indicator> </key_idea> </key_idea_title> <key_idea_title content_type_id="7" content_id="86"> <value>Music</value> <key_idea content_type_id="8" content_id="146"> <value>1- Students will explore and perform music in formal and informal contexts and will improvise, create and perform music based on their own ideas. </value> <performance_indicator content_type_id="9" content_id="357"> <value> create short musical pieces consisting of sounds from a variety of traditional (e.g., tambourine, recorder, piano, voice), electronic (e.g., keyboard) and nontraditional sound sources (e.g., water-filled glasses). </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="363"> <value> explore what musical elements are. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="371"> <value> sing or play simple repeated rhythm patterns with familiar songs and rounds. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="372"> <value> sing songs and play instruments maintaining pitch, rhythm, tone and tempo </value> </performance_indicator> </key_idea> </key_idea_title> <key_idea_title content_type_id="7" content_id="105"> <value>Theatre</value> <key_idea content_type_id="8" content_id="142"> <value>1- Students will create and perform theatre pieces as well as improvisational drama. They will use the basic elements of theatre in their characterizations and improvisations. Students will engage in individual and group theatrical and theatre-related tasks. </value> <performance_indicator content_type_id="9" content_id="365"> <value> imitate experiences through pantomime, play making, dramatic play, story dramatization, story telling and role playing. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="373"> <value> use basic props, simple set pieces and costume pieces to establish place, time and character for the participants. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="374"> <value> use creative drama to communicate ideas and feelings. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="375"> <value> use in individual and group experiences some of the roles, processes and actions for performing and creating theatre pieces and improvisational drama. </value> </performance_indicator> <performance_indicator content_type_id="9" content_id="376"> <value> use language, voice, gesture, movement and observation to express their experiences and communicate ideas and feelings. </value> </performance_indicator> </key_idea> </key_idea_title> </standard> </level></standard_area>If someone could post the XSL script or give some advice that would help a ton. Thanks!!

Link to comment
Share on other sites

this recursive routine will omit any attributes from the copy

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">	<xsl:output method="xml"/>	<xsl:template match="/">		<xsl:apply-templates select="* | node() "/>	</xsl:template>	<xsl:template match="* | node()">		<xsl:copy>			<xsl:apply-templates select="* | node()"/>		</xsl:copy>	</xsl:template></xsl:stylesheet>

Here is an output fragment

<?xml version="1.0"?><standard_area><value>Arts</value><level><value>Alternate</value><standard><value>1-Creating, Performing and Participating in the Arts</value><key_idea_title><value>Movement</value><key_idea><value>happy time harry. </value><performance_indicator><value> create and perform simple dances. </value></performance_indicator><performance_indicator><value> identify and demonstrate movement elements and skills (such as bend, twist, slide, skip, hop, walk in a straight line). </value></performance_indicator><performance_indicator><value> interpret words into a dance. </value></performance_indicator><performance_indicator><value> participate in movement activities. </value></performance_indicator><performance_indicator><value> perform individually or in a group. </value></performance_indicator></key_idea></key_idea_title><key_idea_title>

Note, to include the attributes. use this

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">	<xsl:output method="xml"/>	<xsl:template match="/">		<xsl:apply-templates select="* | @* | node() "/>	</xsl:template>	<xsl:template match="* | @* | node()">		<xsl:copy>			<xsl:apply-templates select="* | @* | node()"/>		</xsl:copy>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

I tried your script and it actually didnt copy the nodes for me... however if anyone else is interested I wrote a script that should remove all attributes from any XML document....<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:attribute-set name="new"> </xsl:attribute-set> <xsl:template match="@*|node()"> <xsl:copy use-attribute-sets="new"> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

Link to comment
Share on other sites

  • 3 weeks later...

I got interested in testing both of your approaches to this. Personally, I feel aalbetski's solution is more readable and as such I would... have prefered it, if it didn't turned out to be slower.With Saxon 8.7, drg1022's solution is almost twice as faster (about 40 000 microseconds faster to be exact). With Stylus Studio's (the program with which I made the test) built in processor, the difference is more unnoticable and varies from one execution to another, so you might say both work fine there. At least for that XML. Oddly enough, in Xalan-J, it seems aalbetski's solution is about 10 000 microseconds faster. Unlike Stylus Studio's processor, this difference is consistent.I guess if you're looking for speed, you have to choose your processor wisely. For readability... go aalbetski.Btw, speaking of performance optimizations, consider self closing the empty attribute-set. That's one node less the XML parser will traverse and reduces file size too. Ahh... just.. here's my performance optimized verison:

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">	<xsl:output method="xml"/>	<xsl:attribute-set name="new"/>	<xsl:template match="*">		<xsl:copy use-attribute-sets="new">			<xsl:apply-templates select="*|text()"/>		</xsl:copy>	</xsl:template></xsl:stylesheet>

It never actually matches an attribute node to begin with.True, it eliminated comments and PIs too, but that's fine with whatever you need the stylesheet for, is it not? If not, yes, you'll need the node() in your apply-teplate's select. Your first version matches attribute nodes and didn't do anything with them (wasting resources). I'm actually surprised aalbetski's solution turned out to be slower, scince his solution doesn't match any attributes either (other then with node(), but that you did too).The tweaked version above runs faster on all XSLT processors in comparrison to aalbetski's solution. I tried tweaking aalbetski's solution too, and the fastest one I got to was:

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">	<xsl:output method="xml"/>	<xsl:template match="*">		<xsl:copy>			<xsl:apply-templates select="*|text()"/>		</xsl:copy>	</xsl:template></xsl:stylesheet>

Which however still runs with about 10 000 microseconds slower on Saxon and Xalan-J. On Stylus Studio's, they are almost equal.

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...