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

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13

    XSLT transform prob along with PHP & CSS


    greetings all,

    i have a simple xml doc which serves as the content of a links page on a web site..

    it goes something like this:

    <?xml version="1.0"?>
    <links.page>
    <link>
    <title>Devshed</title>
    <url>http://www.devshed.com</url>
    </link>
    <link>
    <title>Some Site</title>
    <url>http://www.some.com</url> </link>
    </links.page>

    i have successfully made an xslt to html transformation, however when i attempt to include the xml file in a web page generated with php.. i receive a parsing error.... line 1?????

    in addition, my css style sheet doesn't affect the links generated with xslt. is this suppose to happen?? how can i remedy this?

    also, when viewing the xml doc with netscape 6.2, the only result is the text contained within the xml doc????
    there are quite a few issues here i'm sure, so any help sorting it all out is greatly apprectiated..
    thank you!


    _justin.dago

    PS:
    here is my xsl stylesheet:
    <?xml version="1.0"?>

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <table bgcolor="#999999">
    <xsl:apply-templates/>
    </table>
    </xsl:template>

    <xsl:template match="/links.page">
    <xsl:for-each select="link">
    <tr><td>
    <a class="sub_nav_links"><xsl:attribute name="href">"<xsl:value-of select="url"/>"</xsl:attribute><xsl:value-of select="name"/></a></td></tr>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    13
    Rep Power
    0
    You can put a href in an XSL like this:

    Code:
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:value-of select="url" />
      </xsl:attribute>
      <xsl:value-of select="title" />
    </xsl:element>



    You can even make more compicated href's
    like: index.php?action=insert_product

    Code:
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:value-of select="PHP_SELF"/>?<![CDATA [action=insert_product]]></xsl:attribute>
    	insert new product
        </xsl:element>
    Your XML file should look like this for the above example:

    Code:
    <?xml version=\"1.0\"?>
    	    <main>
    	      <PHP_SELF>$PHP_SELF</PHP_SELF>
    	    </main>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    petrik,

    first let me thank you for the response, and your passing of knowledge to me.

    i have several more questions:

    is my xml file incorrect??
    is your xsl doc an alternative? or the proper way to accomplish my task?
    how bout the parse error when i include() the xml file?? can this not be done?


    now in xsl stylesheet will generate "default" styled links? (that is style defined in a:link) how about css classes?

    justin.dago
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    13
    Rep Power
    0
    Your xml file is correct. I opened it in explorer 5 and it didn't give any errors. Also in your xml file you used 'title' in your xsl file you used 'name'.


    For your classes just add another attribute:

    Code:
    <xsl:element name="a">
      <xsl:attribute name="class">sub_nav_links</xsl:attribute>
      <xsl:attribute name="href">
        <xsl:value-of select="url" />
      </xsl:attribute>
      <xsl:value-of select="title" />
    </xsl:element>
    It's one way to accomplish this and the most flexible.

    Another less flexible (when using URL's like ?action=a&id=b) is:

    Code:
    <a class="sub_nav_links" href="{url}">title</a>
    The following worked for me.:

    xml file:
    Code:
    <?xml version="1.0"?> 
    <links.page> 
    <link> 
    <title>Devshed</title> 
    <url>http://www.devshed.com</url> 
    </link> 
    <link> 
    <title>Some Site</title> 
    <url>http://www.some.com</url> </link> 
    </links.page>
    xsl file:
    Code:
    <?xml version="1.0"?> 
    
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
    <xsl:template match="/"> 
    <table bgcolor="#999999"> 
    <xsl:apply-templates/> 
    </table> 
    </xsl:template> 
    
    <xsl:template match="/links.page"> 
    <xsl:for-each select="link"> 
    <tr><td> 
    <xsl:element name="a">
      <xsl:attribute name="class">sub_nav_links</xsl:attribute>
      <xsl:attribute name="href">
        <xsl:value-of select="url" />
      </xsl:attribute>
      <xsl:value-of select="title" />
    </xsl:element>
    </td></tr> 
    </xsl:for-each> 
    </xsl:template> 
    </xsl:stylesheet>

    php file (with sablotron enabled):
    Code:
    <?php
    
    // store XML and XSL content as variables
    $xmlstring = join('', file(test.xml'));
    $xslstring = join('', file('test.xsl'));
    
    // call the XSLT processor directly
    xslt_process($xslstring, $xmlstring, $result);
    
    // output the result
    echo $result;
    ?>
    Last edited by petrik; July 18th, 2002 at 02:53 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    ahhh... didn't see the name/ title thing, that might not be in my original, i have to go back and check. it may also be why the links weren't exactly correct...

    once again thank you...
    so i just had to take it one step further and set another attribute.. got it

    the sablotron code is to link the xml files to the php script???

    and as for sablotron, this poses another question, which has been on my mind..

    do you recommend any particular method for processing xml docs...

    sax, dom, xsl, don't know really anything about sablotron, (i'll have to do some reading, think there's a few articles on devshed) but...
    (i may be mixing some technologies...)
    do these all accomplish relatively the same tasks???

    right now, i'm pretty new to xml and its friends. this links doc is academic..it's meant for a personal site..to sort of demonstrate that i am capable of working with xml to perspective employers...
    can you or anyone recommend some other ways of incorporating xml into my site???

    youngpup.net makes some cool use of xml, i believe he generates his tree style navigation with xml???
    do you or anyone know of any resources(tutorial, etc.) that show how to create this navigation system??

    there are quite a few questions here, and i apologize for the length of the response, but how will i ever learn anything?

    any help is greatly appreciated!!

    justin.dago
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    13
    Rep Power
    0
    Sablotron is a xml/xslt parser. It's optimized for parsing a xml file through a xslt file.
    If this is what you you will be doing, I'd recommend Sablotron. (Just make sure your Host supports Sablotron.)

    The 3 pieces of code are all you need for parsing the xml.

    The Sax and Dom parsers are for reading different xml file's where you don't know the exact structure and/or tags.

    Sax: reads line for line.
    Dom: reads the whole document and knows what are all the parents and children. And it is there for much more CPU intensive.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    petrik,

    did you happen to check that site:
    youngpup.net??

    any thoughts on other uses for xml in my site??

    read the sablotron article, so i'm straight on that, pretty nice, and pretty simple.

    justin.dago

IMN logo majestic logo threadwatch logo seochat tools logo