SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (XSL) Simple XML Output - Help Needed!

    Hi guys,

    I'm in the process of learning about XML/XSL and am having trouble outputting some XML onto a simple XSL page. The two pages are shown below (condensed to the important parts):

    XML File
    Code:
    <?xml version='1.0' encoding='ISO-8859-9'?>
    <?xml-stylesheet type="text/xsl" href="ServiceInformation.xsl"?>
    <TVAMain xmlns='urn:tva:metadata:2005' xmlns:mpeg7='urn:tva:mpeg7:2005' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xml:lang='en'>
      <ProgramDescription>
        <ServiceInformationTable>
          <ServiceInformation serviceId='BBCOne'>
            <Name>BBC One</Name>
          </ServiceInformation>
          <ServiceInformation serviceId='BBCTwo'>
            <Name>BBC Two</Name>
          </ServiceInformation>
        </ServiceInformationTable>
      </ProgramDescription>
    </TVAMain>
    XSL File
    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <?xml-stylesheet type="text/css" href="assignment1.css"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bbc='urn:tva:metadata:2005' version="1.0">
    
    <xsl:template match="/">
    	<xsl:apply-templates select="document('ServiceInformation.xml')/bbc:TVAMain/bbc:ProgramDescription/bbc:ServiceInformationTable"/>
    </xsl:template>
    
    <xsl:template match="bbc:ServiceInformationTable">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My Page</title>
    </head>
    <body>
    <div id="container">
        
        <xsl:apply-templates select="bbc:ServiceInformation"/>
        
    </div>
    </body>
    </html>
    </xsl:template>
    
    <xsl:template match="bbc:ServiceInformation">
      <xsl:value-of select="bbc:Name"/><br />
    </xsl:template>
    
    </xsl:stylesheet>
    Any help/pointers would be greatly appreciated

    Thanks,
    Will
    My portfolio: lemonadeX.net
    XHTML/CSS/PHP coder for hire

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Given your xml, I think that this should be your xsl:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                    xmlns:bbc='urn:tva:metadata:2005'
                    version="1.0">
                    
    <xsl:output method="html" 
                encoding="UTF-8" 
                indent="yes" 
                doctype-public="-//W3C//DTD HTML 4.01//EN" 
                doctype-system="http://www.w3.org/TR/html4/strict.dtd" 
                standalone="yes" 
                omit-xml-declaration="yes"
      />
    
      <xsl:template match="/">
        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>My Page</title>
        </head>
        <body>
          <div id="container">
            <xsl:apply-templates/>
          </div>
        </body>
      </xsl:template>
    
      <xsl:template match="bbc:ServiceInformation">
        <xsl:value-of select="bbc:Name"/><br />
      </xsl:template>
    
    </xsl:stylesheet>
    First: when you have an xmlns= in the xml, it becomes the default namesapce, but in the xsl, you have to give the same uri with a handle (here, bbc).
    Then, you must prefix with that handle every access to nodes.

    Second, apply-templates goes to all the childrens of a node.
    If you do not need to create content, don't create a template for it.

    Third, you define the doctype and the output type into the xslutput directive

    Those are the obvious errors I've seen you did had in your stylesheet, but I'm not a guru, I may have missed a few...

  3. #3
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi tripy,

    Thank you very much for your reply That has definitely helped me understand it more, but after making the suggested changes (as well as some of my own) it remains as a blank page with no data showing up from the XML file =/

    Any suggestions that anyone can offer are much appreciated

    Thanks,
    Will
    My portfolio: lemonadeX.net
    XHTML/CSS/PHP coder for hire

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post your xml and xsl files, and I will be able to help you.
    Xml with specific namespaces are a bit tricky.

  5. #5
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    The pages look as follows, like I say similiar to what you posted but still not showing any data unfortunately:

    XML
    Code:
    <?xml version='1.0' encoding='ISO-8859-9'?>
    <?xml-stylesheet type="text/xsl" href="ServiceInformation.xsl"?>
    <TVAMain xmlns='urn:tva:metadata:2005' xmlns:mpeg7='urn:tva:mpeg7:2005' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xml:lang='en'>
      <ProgramDescription>
        <ServiceInformationTable>
          <ServiceInformation serviceId='BBCOne'>
            <Name>BBC One</Name>
          </ServiceInformation>
          <ServiceInformation serviceId='BBCTwo'>
            <Name>BBC Two</Name>
          </ServiceInformation>
        </ServiceInformationTable>
      </ProgramDescription>
    </TVAMain>
    XSL
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    				xmlns:bbc='urn:tva:metadata:2005' 
                    version="1.0">
    
    <xsl:output method="html"
    	encoding="UTF-8"
        indent="yes"
        doctype-public="-//W3C//DTD HTML 4.01//EN"
        doctype-system="http://www.w3.org/TR/html4/strict.dtd"
        standalone="yes"
        omit-xml-declaration="yes">
    
    <xsl:template match="TVAMain/ProgramDescription/ServiceInformationTable/ServiceInformation">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My Page</title>
    </head>
    <body>
    <center><div id="container">
        
        <xsl:apply-templates />
        
    </div></center>
    </body></html>
    </xsl:template>
    
    <xsl:template match="TVAMain/ProgramDescription/ServiceInformationTable/ServiceInformation" select="document('ServiceInformation.xml')">
      <xsl:value-of select="Name"/><br />
    </xsl:template>
    
    </xsl:output>
    </xsl:stylesheet>
    Thanks again
    My portfolio: lemonadeX.net
    XHTML/CSS/PHP coder for hire

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...
    first, in your xsl:
    Code:
    <xsl:output method="html"
        encoding="UTF-8"
        indent="yes"
        doctype-public="-//W3C//DTD HTML 4.01//EN"
        doctype-system="http://www.w3.org/TR/html4/strict.dtd"
        standalone="yes"
        omit-xml-declaration="yes">
    xsl:output is a processing instruction. You don't close it at the end of the document.
    You must keep it self closed. Do not put the rest of the stylesheet into it.
    The correct syntax is
    Code:
    <xsl:output method="html"
        encoding="UTF-8"
        indent="yes"
        doctype-public="-//W3C//DTD HTML 4.01//EN"
        doctype-system="http://www.w3.org/TR/html4/strict.dtd"
        standalone="yes"
        omit-xml-declaration="yes"/>
    without the
    Code:
    </xsl:output>
    at the end.

    Code:
    <xsl:template match="TVAMain/ProgramDescription/ServiceInformationTable/ServiceInformation">
    is wrong. You don't give an XPath query as the name of a template.
    But your template name must match (if you don't use call-template but apply-templates) the name of the final element to be matched

    Then, in your first template, you have put
    Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    Wrong again. In the xsl:output element, you have already specified that you wanted an html ouptut with a specific system doctype. You don't have to re declare it here.

    And finally, at the end of your template, you have this:
    Code:
    xsl:template match="TVAMain/ProgramDescription/ServiceInformationTable/ServiceInformation" select="document('ServiceInformation.xml')">
    So, again, the template name should not be an xpath query.
    What strike me is that this template seems to have the same name that the first.
    You cannot do that. Each template must be unique.
    And finally, I never have seen anything like this document() function. Looking for it, I see that it's used to reference an external xml document.
    So, in that field, you are alone. I never have used anything like this.

    So, after those remarks, your corrected xsl document should be, I think:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                    xmlns:bbc='urn:tva:metadata:2005' 
                    version="1.0">
    
    <xsl:output method="html"
        encoding="UTF-8"
        indent="yes"
        doctype-public="-//W3C//DTD HTML 4.01//EN"
        doctype-system="http://www.w3.org/TR/html4/strict.dtd"
        standalone="yes"
        omit-xml-declaration="yes"/>
    
    <!-- You always start by declaring your root element, and you start from it-->
    <xsl:template match="/">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My Page</title>
    </head>
    <body>
    <center>
      <div id="container">
        <xsl:apply-templates />
      </div>
    </center>
    </body>
    </xsl:template>
    
    <!-- now, if you want to handle only informations in the ServiceInformation nodes, then you simply refer to it by the name
    Now, as the root element has an xmlns attribute, it means that every nodes you want to access below it must be prefixed with the handle corresponding to the URI of the namespace.
    IE: if in yml you have
      <TVAMain xmlns='imgonna_blowthis'>
      as the root element, you will need to declare in your xsl:
      <xsl:stylsheet xmlns:anything_i_want='imgonna_blowthis'>
      The resolution of the name space is done on "imgonna_blowthis"
    -->
    <xsl:template match="bbc:ServiceInformation" select="document('ServiceInformation.xml')">
      <!-- As for the template name, you need to prefix this too-->
      <xsl:value-of select="bbc:Name"/><br />
    </xsl:template>
    
    </xsl:stylesheet>
    I have left your xml untouched, and applying this stylesheet to it produced:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <head xmlns:bbc="urn:tva:metadata:2005">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>My Page</title>
    </head><body xmlns:bbc="urn:tva:metadata:2005"><center><div id="container">
      
        
          BBC One<br>
          BBC Two<br>
        
      
    </div></center></body>
    By the way, if you work on a linux machine, and you have libxslt2 installed, you can use this command to test your xml/xsl:
    Code:
    xsltproc file.xml file.xsl
    It will print the result on the terminal windows.

  7. #7
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi tripy,

    I tried making the modifications you specified (and also tried copy/pasting everything in the above 'corrected xsl document' into my XSL file (left my XML file untouched) and navigated to the .xsl file in 3 different browsers (Chrome, IE, Firefox) - all returned different results (typical :P) but strangely none displayed the data.

    Firefox returned an error saying the XML sheet has no style sheet associated with it. I fixed this by adding the following line below the 'version' (1st) line on the XSL page:

    Code:
    <?xml-stylesheet type="text/css" href="mycssfile.css"?>
    Now it just displays the title ('My Page') in plain text, same as Chrome. IE seems to understand the CSS and displays the correct layout of 'container' (essentially a blue box in which the data should be contained).

    But if you're using the code exactly as above and getting it to output the data, I feel as though I'm making a stupid error somewhere? Is it as simple as loading the .xsl file into the browser, ensuring the XML and XSL files are in the same directly on my local web server?

    Thanks again for your patience

    Will
    My portfolio: lemonadeX.net
    XHTML/CSS/PHP coder for hire

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firefox returned an error saying the XML sheet has no style sheet associated with it. I fixed this by adding the following line below the 'version' (1st) line on the XSL page:
    You are mixing 2 notions here...
    The style sheet that Firefox complains about is the XSL stylesheet, not a css stylesheet.
    But in the light of what you said after, I think the error is that you try to load the stylesheet rather that the xml document.

    I feel as though I'm making a stupid error somewhere? Is it as simple as loading the .xsl file into the browser, ensuring the XML and XSL files are in the same directly on my local web server?
    :-D
    Almost... You must load the XML file in the browser, not the XSL.
    Once you loaded the XML, the browser will fetch the XSL at the url specified in the xsl:stylesheet processor instruction and display the transformed document.

  9. #9
    SitePoint Evangelist LemoNade's Avatar
    Join Date
    Aug 2005
    Location
    Southampton, UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Words cannot describe how stupid I feel right now

    Thanks again for the help tripy, you've helped me understand XSL a lot more!!

    Regards,
    Will
    My portfolio: lemonadeX.net
    XHTML/CSS/PHP coder for hire

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problems.
    And if you feel a bit more curious about it, as I see that you do PHP too, I've put together an helper class to use xsl as a template engine.
    You create xml from php, and declare which stylesheet it should be associated with.
    The class detects if the browser support xsl transformations, and if it does not, transform it on the server

    http://www.quixml.org


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •