#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    XSLT transformation not giving the desired results


    i want to transform xml file yusuf.xml collapsing all elements like suraid,verseid etc except <yusuf> as attributes:
    Code:
    <ahmed>
    		<suraid>1</suraid>
    		<verseid>3</verseid>
    		<groupcode>1.2-4</groupcode>
    		<yusuf>Most Gracious, Most Merciful;</yusuf>
    	</ahmed>
    	<ahmed>
    		<suraid>1</suraid>
    		<verseid>4</verseid>
    		<groupcode>1.2-4</groupcode>
    		<yusuf>Master of the Day of Judgment.</yusuf>
    	</ahmed>
    above code should look like
    Code:
    <quran suraid="1" verseid="3" groupcode="1.2-4" author="yusuf">
    Most Gracious, Most Merciful;
    </quran>
    <quran suraid="1" verseid="4" groupcode="1.2-4" author="yusuf">
    Master of the Day of Judgment;
    </quran>

    see to it that i want the verse contents in the <quran> tag

    below is my schema xsd for above ,its not giving the desired result :
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- edited with XMLSpy v2011 rel. 2 (x64) (http://www.altova.com) by aliabbas (puu) -->
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    	<xs:element name="quran">
    		<xs:complexType>
    			<xs:simpleContent>
    				<xs:extension base="xs:string">
    					<xs:attribute name="verseid" type="xs:unsignedShort"/>
    					<xs:attribute name="suraid" type="xs:unsignedByte" use="required"/>
    					<xs:attribute name="groupcode" type="xs:string"/>
    					<xs:attribute name="author" type="xs:string" use="required"/>
    				</xs:extension>
    			</xs:simpleContent>
    		</xs:complexType>
    	</xs:element>
    </xs:schema>
    following is the transformation xsl file
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    	<xsl:template match="/">
    		<xsl:variable name="var1_dataroot" select="dataroot"/>
    		<quran>
    			<xsl:attribute name="xsi:noNamespaceSchemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance">C:/Users/ALI/Documents/transform.xsd</xsl:attribute>
    			<xsl:for-each select="$var1_dataroot/ahmed/verseid">
    				<xsl:attribute name="verseid">
    					<xsl:value-of select="string(floor(number(string(.))))"/>
    				</xsl:attribute>
    			</xsl:for-each>
    			<xsl:for-each select="$var1_dataroot/ahmed/suraid">
    				<xsl:attribute name="suraid">
    					<xsl:value-of select="string(floor(number(string(.))))"/>
    				</xsl:attribute>
    			</xsl:for-each>
    			<xsl:for-each select="$var1_dataroot/ahmed/groupcode">
    				<xsl:attribute name="groupcode">
    					<xsl:value-of select="string(.)"/>
    				</xsl:attribute>
    			</xsl:for-each>
    		</quran>
    	</xsl:template>
    </xsl:stylesheet>
    i used mapforce but i cant get the contents of each verse in individual <quran> tag

    and i am getting only one <quran > instance as output
    please help me
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    You need to do a (just one) for-each on <ahmed>.
    Code:
    for-each ahmed
    	attribute suraid: value-of suraid
    	attribute verseid: value-of verseid
    	attribute groupcode: value-of groupcode
    	value-of yusuf
    Or you can use a template.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    i urgently need a solution


    thanx
    where to execute the above code
    and should i type exactly like this ?
    i am new to xsl etc and i donno xpath much

    i am using xml spy software but i donno much abt xslt stylesheets ,xquery
    please help me i need to finish this task fast for further research work
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0
    i got it !! see beloew
    but i want to repeat the procedure for multiple files how to automate this proc?'


    <?xml version="1.0" encoding="UTF-8"?>
    Code:
    <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="dataroot">
    		<collection>
    			<xsl:for-each select="ahmed">
    				<quran>
    					<xsl:attribute name="suraid">
    						<xsl:value-of select="suraid"/>
    					</xsl:attribute>
    					<xsl:attribute name="verseid">
    						<xsl:value-of select="verseid"/>
    					</xsl:attribute>
    					<xsl:attribute name="groupcode">
    						<xsl:value-of select="groupcode"/>
    					</xsl:attribute>
    					<xsl:attribute name="author">
    						<xsl:text>shakir</xsl:text>
    					</xsl:attribute>
    					<xsl:value-of select="shakir"/>
    				</quran>
    			</xsl:for-each>
    		</collection>
    	</xsl:template>
    </xsl:stylesheet>
  8. #5
  9. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    Then your best bet is to write some kind of not-XSLT script to get the files and run them each through an XSLT processor.

    For example, with PHP you can write something that uses glob and XSLTProcessor.

IMN logo majestic logo threadwatch logo seochat tools logo