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

XML/XSL/XSLT Question

Options

Comments

  • Registered Users Posts: 2,494 ✭✭✭kayos


    Right here is the question I want to display data from my XML document using XSL. So far so good this is working nicely because the Client PC recieves only pure HTML so the Pages produced will be browser independant. But heres my problem

    I have a XML Document with say the following layout
    <Articles>
    	<ArticleTitle>Test Title!</ArticleTitle>
    	<ArticleAuthor>kayos</ArticleAuthor>
    	<ArticleDate>29 March 2001</ArticleDate>
    	<ArticleText>This will be the article text here</ArticleText>
    </Articles>
    
    Right then I apply my XSL style sheet. Everything works fine in the example of xml shown.
    But if the <ArticleText> Element say had a link in it it all falls flat on its face. What I want to know is does anyone out there have a way of doing this IN xml /xsl /xslt???

    kayos


  • Registered Users Posts: 332 ✭✭spod


    add xhtml or the new mini xhtml uri to your xsl documents namespace, or possibly your xml document, it's too late and i'm tired and my big xml wotsit book is in the other room.

    implementation is possibly slightly dependant on your parser, not sure, i'm a bit rusty on the latest of the latest on the xml front and I remember that there was alot of hassle about namespaces.

    Anyway, once xhtml is in your document namespace then you can have both valid tags for your article dtd and calid xhtml tags in the xml document you parse.

    That way you can also have the added benefit of knowing that any and all html included in the article has to be fully w3c xhthml compliant.

    If talliesin hasn't posted a proper answer by Monday lunchtime I'ill try to remember to do it smile.gif


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Any one have any more ideas on this?? I have tried a few things but they never seem to work. One thing I got close with was as follows. Here is my xml doc
    &lt;Articles&gt;
    	&lt;Article&gt;
    		&lt;ArticleTitle&gt;Test Title&lt;/ArticleTitle&gt;
    		&lt;ArticleAuthor&gt;kayos&lt;/ArticleAuthor&gt;
    		&lt;ArticleDate&gt;29 March 2001&lt;/ArticleDate&gt;
    		&lt;ArticleText&gt;Test Text with link. &lt;url ref ="http://www.w3.org/XML"&gt;heloo&lt;/url&gt;&lt;/ArticleText&gt;
    	&lt;/Article&gt;
    &lt;/Articles&gt;
    
    then in the xsl I did something like
     &lt;?xml version="1.0"?&gt; 
    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"&gt;
    
    
    &lt;xsl:template match="/"&gt;
    	&lt;xsl:for-each select="Articles/Article"&gt;
    		&lt;TABLE bgColor="#CED6DE" border="2" borderColor="#ff9900" cellPadding="2" cellSpacing="0" width="550" align="center"&gt;
    		&lt;TBODY&gt;
    		&lt;TR align="left" vAlign="center"&gt;
    		&lt;TD bgColor="#6767A3" width="450"&gt;&lt;B&gt;&lt;FONT color="#ffffff" face="Arial, Helvetica, sans-serif" size="3"&gt;&lt;xsl:value-of select="ArticleTitle"/&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/TD&gt;
    		&lt;TD align="right" bgColor="#6767A3" width="100"&gt;&lt;FONT color="#ffffff" face="Arial, Helvetica, sans-serif" size="1"&gt;&lt;xsl:value-of select="ArticleAuthor"/&gt;&lt;BR/&gt;
    		&lt;xsl:value-of select="ArticleDate"/&gt;&lt;/FONT&gt;&lt;/TD&gt;
    		&lt;/TR&gt;
    
    		&lt;TR&gt;
    		&lt;TD colSpan="2"&gt;
    		&lt;P&gt;&lt;FONT color="#000000" face="Arial, Helvetica, sans-serif" size="2"&gt;&lt;xsl:apply-templates select="ArticleText"/&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/TD&gt;
    		&lt;/TR&gt;
    		&lt;/TBODY&gt;
    		&lt;/TABLE&gt;
    		&lt;P&gt;&#160;&lt;/P&gt;
    	&lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="ArticleText"&gt;
    &lt;xsl:apply-templates/&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="url" priority="1"&gt;
    &lt;A href="{@href}"&gt;&lt;xsl:apply-templates/&gt;&lt;/A&gt;
    &lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;
    

    I've played with a few varations of this but none of them work for me does anyone have any ideas

    kayos


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    The text inside a node is considered a node in itself, and can be accessed by looking for a node returned by "text()" (the exact way to return it may vary in XSL Dom implementations)
    So you can view the code:
    <ArticleText>Test Text with link. <url ref ="http://www.w3.org/XML">heloo</url></ArticleText&gt;

    as equivalent to:

    <ArticleText>
    <text()>Test Text with link.</text()>
    <url ref ="http://www.w3.org/XML">heloo</url&gt;
    </ArticleText>

    or if you need to watch out for other elements imbeded in the <url> node (e.g. in <a> in xhtml you can have any other "inline" node except for another <a>. you can consider it equiv. to:

    <ArticleText>
    <text()>Test Text with link.</text()>
    <url ref ="http://www.w3.org/XML"&gt;
    <text()>heloo</text()></url>
    </ArticleText>

    so try something like:

    <xsl:template match="ArticleText">
    <xsl:apply-templates select="text()|url" />
    </xsl:template>
    <xsl:template match="text()">
    <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="url">
    <xsl:element name="a">
    <xsl:attribute name="href">
    <xsl:value-of select="@href&quot;>
    </xsl:attribute>
    <xsl:apply-templates select="."/>
    </xsl:element>
    </xsl:template>


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    BTW, the capital letters in the element names of your XHTML may confuse some XSL Doms.


  • Advertisement
Advertisement