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

    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    Tranform xml with xsl issue


    XML code

    Code:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="investment.xsl"?>
    <investment>
    	<type>stock</type>
    	<name>Microsoft</name>
    	<price type="high">100</price>
    	<price type="low">94</price>
    </investment>
    XSL code

    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    	<xsl:template match="investment">
    		<xsl:element name="{type}">
    			<xsl:attribute name="name" ><xsl:value-of select="name"/></xsl:attribute>
    			<xsl:for-each select="price">
    				<xsl:attribute name="{@type}" ><xsl:value-of select="."/></xsl:attribute>
    			</xsl:for-each>
    		</xsl:element>
    	</xsl:template>
    </xsl:stylesheet>
    Expected output
    Code:
    <?xml version="1.0"?>
    <stock name="Microsoft" high="100" low="94"/>
    I'm having issue transforming my XML file using the XSL stylesheet. I've link my XSL stylesheet with my XML and when I open my XML file I get a blank page instead of the expected output. Can anyone help me please?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    191
    Rep Power
    49
    xsl is no programming langauge

    think xsl as template

    when node is found write what is doing

    not use for-each

    when xslt parser run xslt file was in first loop
    see here template

    Code:
    <xsl:template match="price">
    in node price
    Code:
    <price type="low">94</price>
    are information in attribut and in node self
    so you must write to get information
    Code:
                    <xsl:attribute name="{@type}">
    			<xsl:value-of select="."/>
    		</xsl:attribute>
    xsl
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output indent="yes" method="xml"/>
    	<xsl:template match="/">
    		<xsl:element name="stock">
    			<xsl:attribute name="name">
    				<xsl:apply-templates select="investment"/>
    			</xsl:attribute>
    			<xsl:apply-templates select="investment/price"/>
    		</xsl:element>
    	</xsl:template>
    	<xsl:template match="investment">
    		<xsl:value-of select="name"/>
    	</xsl:template>
    	<xsl:template match="price">
    		<xsl:attribute name="{@type}">
    			<xsl:value-of select="."/>
    		</xsl:attribute>
    	</xsl:template>
    </xsl:stylesheet>
    result
    Code:
    <?xml version='1.0' ?>
    <stock name="Microsoft" high="100" low="94"/>
    Helmut Hagemann Germany

    fallen to the bottom of the facts?
    I reach my hand and we go together


    wer lesen und google kann ist klar im Vorteil
    who read and google is able is clear in the advantage

IMN logo majestic logo threadwatch logo seochat tools logo