xml - Extracting & Converting Attributes into Elements using XSLT -



xml - Extracting & Converting Attributes into Elements using XSLT -

i have next xml:

<?xml version="1.0" encoding="utf-8"?> <rdate date="2014-03-14"> <student id="a-001" sex="m" group_a="yellow" group_b="green"> <name id="a-001-1" student_id="a-001" group="yellow"> <l_name>yuvraj</l_name> <m_name/> <f_name>bhatia</f_name> <o_name/> <age>15</age> <ethnicity>asian</ethnicity> </name> <origin id="a-001-a-o" region="asia"> <birth>1999-01-01</birth> <place>mumbai</place> </origin> <ident id="a-001-a-o-i" type="paper/photo"> <passport/> <country/> </ident> </student> <student id="a-002" sex="f" group_a="green" group_b="blue"> <name id="a-002-1" student_id="a-002" group="green"> <l_name>singh</l_name> <m_name/> <f_name>palwinder</f_name> <o_name>palwinder singh</o_name> <age>19</age> <ethnicity>asian</ethnicity> </name> <name id="a-002-2" student_id="a-002" group="green"> <l_name>singh</l_name> <m_name>balwant</m_name> <f_name>palwinder</f_name> <o_name>balwant singh</o_name> <age>19</age> <ethnicity>asian</ethnicity> </name> </student> </rdate>

which gives me next output:

<?xml version="1.0" encoding="utf-8"?><rdate><date>2014-03-14</date> <student><id>a-001</id><sex>m</sex><group_a>yellow</group_a><group_b>green</group_b> <name><id>a-001-1</id><student_id>a-001</student_id><group>yellow</group> <l_name>yuvraj</l_name> <m_name/> <f_name>bhatia</f_name> <o_name/> <age>15</age> <ethnicity>asian</ethnicity> </name> <origin><id>a-001-a-o</id><region>asia</region> <birth>1999-01-01</birth> <place>mumbai</place> </origin> <ident><id>a-001-a-o-i</id><type>paper/photo</type> <passport/> <country/> </ident> </student> <student><id>a-002</id><sex>f</sex><group_a>green</group_a><group_b>blue</group_b> <name><id>a-002-1</id><student_id>a-002</student_id><group>green</group> <l_name>singh</l_name> <m_name/> <f_name>palwinder</f_name> <o_name>palwinder singh</o_name> <age>19</age> <ethnicity>asian</ethnicity> </name> <name><id>a-002-2</id><student_id>a-002</student_id><group>green</group> <l_name>singh</l_name> <m_name>balwant</m_name> <f_name>palwinder</f_name> <o_name>balwant singh</o_name> <age>19</age> <ethnicity>asian</ethnicity> </name> </student> </rdate>

when transformed using next xslt:

<?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:element name="{name()}"> <xsl:for-each select="@*"> <xsl:element name="{name()}"> <xsl:value-of select="."/> </xsl:element> </xsl:for-each> <xsl:apply-templates select="*|text()"/> </xsl:element> </xsl:template> </xsl:stylesheet>

but looking forwards next output:

<?xml version="1.0" encoding="utf-8"?> <data> <students> <student> <unique-id>a-001</unique-id> <gender>male</gender> <name_a>bhatia</name_a> <name_b/> <name_c>yuvraj</name_c> <comments> <group_a>yellow</group_a> <group_b>green</group_b> <student_id>a-001</student_id> <ident_id>a-001-a-o-i</ident_id> <ident_type>paper/photo</ident_type> </comments> <born> <date> <a>1999-01-01</a> </date> </born> <idents> <ident> <passport/> <country/> </ident> </idents> </student> </students> </data>

what must noted here that:

my xml has more 5000 unique students. each pupil may have 1 or more "name" attributes/elements (as depicted in sec student) along corresponding elements. each pupil may have 1 or more "origin" attributes/elements along corresponding elements. each pupil may have 1 or more "ident" attributes/elements along corresponding elements. additional attributes may or may not exist in original xml. such incorporated under "comments" element after transformation.

i not sure difficulty here: seems exercise in basic xslt. need much more explicit (and verbose). see if can started:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <data> <students> <xsl:apply-templates select="rdate/student"/> </students> </data> </xsl:template> <xsl:template match="student"> <student> <unique-id><xsl:value-of select="@id"/></unique-id> <gender> <xsl:choose> <xsl:when test="@sex='m'">male</xsl:when> <xsl:otherwise>female</xsl:otherwise> </xsl:choose> </gender> <!-- , on ... --> </student> </xsl:template> </xsl:stylesheet>

xml xslt

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -