Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

XSLT Question

Options
  • 19-04-2005 6:06pm
    #1
    Registered Users Posts: 160 ✭✭


    Hi,

    A quick and hopefully straight forward question for an XSLT expert....

    Basically I have a XML file something like this:

    <?xml version="1.0"?>
    <form>
    <name useParam="yes" paramName="fullName">John Murphy</name>
    <age>28</age>
    <phone useParam="yes" paramName="phoneNumer">01-34323223</phone>
    </form>

    Now, I need to translate this into a file that keeps all elements that have useParam="yes" and give them an element name = paramName.
    So my output should look like this:

    <?xml version="1.0"?>
    <form>
    <fullName>John Murphy</fullName>
    <phoneNumer>01-34323223</phoneNumer>
    </form>

    Any help would be greatly appreciated.

    Finn


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    well first of all I don't like using attributes in XML. Why not just add it as a node ? makes it much easier to read and parse. Based on data provided below would it be possible for useParam to be Yes for name but No for Phone ? If you had just added it as a node then you wouldn't have this issue and your file would also be smaller.
    That said I have added some extra test data so the xml file looks like
    <?xml version="1.0"?>
    <forms>
    	<form>
    		<name useParam="yes" paramName="fullName">John Murphy</name>
    		<age>28</age>
    		<phone useParam="yes" paramName="phoneNumer">01-34323223</phone>
    	</form>
    	<form>
    		<name  useParam="no" paramName="fullName">Eamonn Murphy</name>
    		<age>28</age>
    		<phone useParam="no" paramName="phoneNumer">01-43</phone>
    	</form>
    	<form>
    		<name useParam="yes" paramName="fullName">Eamonn</name>
    		<age>28</age>
    		<phone useParam="yes" paramName="phoneNumer">01-44</phone>
    	</form>
    </forms>
    
    Basically to get at the attribute you need to use @ variable. For a attribute in the current node which would be form you could just use @attributename but as we are working on attributes inside form we need to user node/@attributename. Once we have the attribute name a simple xsl:if lets use pick only the rows we need. The CDATA construct is then used to write the xml format strings to prevent errors in transformation.
    The xsl below does what you want.

    Is this homework/project or for work ? If work I can provide XSL/XML, SQL consultancy etc :) just pm me
    <?xml version="1.0" encoding="UTF-8"?>
    <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="/">
    	<![CDATA[<?xml version="1.0"?>]]>
    	<xsl:for-each select="forms/form">
    	<xsl:if test="name/@useParam[not(.='no')]">
    	<![CDATA[<form><fullName>]]><xsl:value-of select="name"></xsl:value-of><![CDATA[</fullName>]]>
    	<![CDATA[<phoneNumber>]]><xsl:value-of select="phone"></xsl:value-of>
    	<![CDATA[</phoneNumer></form>]]>
    	</xsl:if>
    	</xsl:for-each>
    	</xsl:template>
    </xsl:stylesheet>
    


  • Registered Users Posts: 160 ✭✭Finn Dub


    That's excellent Amen, thanks a million. Its for work so I will definetly keep you in mind if we need a consultant.


Advertisement