Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 4th, 2006, 03:23 PM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default need help to get the node value

Hi,

how to get the item's <title>, <link>, and <description>? Thanks.


http://www.rfa.org/english/china/rss.xml

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/rdf:RDF">

    <xsl:for-each select="*[name() = 'item']">
    <strong>
    <a href="{link}" target="_main">
   <xsl:value-of select="./item/title"/>
    </a>
    </strong><br></br>

    <xsl:value-of select="description"/>
    <br></br>

  </xsl:for-each>


</xsl:template>


</xsl:stylesheet>


 
Old May 5th, 2006, 03:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Processing RDF isn't much fun because of the two different versions with different namespaces, presumably that accounts for the select="*[name() = 'item']". My preferred approach is to preprocess it so everything is in the same namespace.

Your reference to "link" doesn't work because link is in a namespace: to reference an element in a namespace you need to use p:link and then declare the namespace xmlns:p in the stylesheet.

Your reference to "title" doesn't work because you've got the context node wrong. "./item/title" means "child::item/child::title" and since you're already on an item, you don't want to look for item children. And the same thing about namespace prefixes applies here.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 5th, 2006, 08:52 AM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael,

I still couldn't get it to work :(

please help again,

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/rdf:RDF">

    <xsl:for-each select="*[name() = 'item']">

     <a href="{rdf:link}" target="_main">
   <xsl:value-of select="rdf:title"/>
    </a>
    <br></br>
    <xsl:value-of select="rdf:description"/>
  </xsl:for-each>

</xsl:template>

</xsl:stylesheet>



 
Old May 5th, 2006, 08:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The link and title attributes are not in the RDF namespace they are in the namespace http://purl.org/rss/1.0/. You need something like this:

<xsl:template match="/rdf:RDF">

    <xsl:for-each select="*[name() = 'item']"
           xmlns:rss="http://purl.org/rss/1.0/">

     <a href="{rss:link}" target="_main">
   <xsl:value-of select="rss:title"/>
    </a>
    <br></br>
    <xsl:value-of select="rss:description"/>
  </xsl:for-each>

</xsl:template>

</xsl:stylesheet>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 5th, 2006, 09:04 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The full name of the link element is {http://purl.org/rss/1.0/}link because there is no prefix set for this namespace therefore unprefixed elements in the source XML fall into this group.

You need to declare xmlns:purl="http://purl.org/rss/1.0/" in your stylesheet element and then use purl:link, not rdf:link to select them.

Suggest you read

http://www.dpawson.co.uk/xsl/sect2/N5536.html

especially numbers 13 and 23.

--

Joe (Microsoft MVP - XML)
 
Old May 5th, 2006, 09:42 AM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks for the help.

it works now.

how to get rid of the <a href=...></a> from the description node, for examle, get rid of chars before "Groups of parents throughout China have formed a last-ditch..."?


http://www.rfa.org/english/china/rss.xml
here is the item struture:
<item rdf:about="http://www.rfa.org/english/news/2006/05/03/china_children">
<title>Missing, Presumed Sold: Chinese Parents' Desperate Hunt For Their Kids</title>
<link>http://www.rfa.org/english/news/2006/05/03/china_children</link>
<description>&lt;a href="http://www.rfa.org/english/news/2006/05/03/china_children"/>&lt;img src="http://www.rfa.org/english/images/2006/05/03/ChinaChildren75.jpg" height="75" width="75" alt="Chinese Children"/>&lt;/a>Groups of parents throughout China have formed a last-ditch alliance in the hope of recovering their missing children, who are believed to have been kidnapped and sold in a booming adoption, bridal, and ************ trade in human beings.</description>
<dc:date>2006-05-03T10:33:00-0400</dc:date>
</item>



xslt:
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rss="http://purl.org/rss/1.0/"
>

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/rdf:RDF">

    <xsl:for-each select="*[name() = 'item']">

     <a href="{rss:link}" target="_main">
   <xsl:value-of select="rss:title"/>
    </a>
    <br></br>
    <xsl:value-of select="rss:description"/>
  </xsl:for-each>

</xsl:template>

</xsl:stylesheet>


 
Old May 5th, 2006, 09:57 AM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

or how to translate so the output html will dispay an image directly in the page instead of

...
<a href="http://www.rfa.org/english/news/2006/05/03/china_children"/><img src="http://www.rfa.org/english/images/2006/05/03/ChinaChildren75.jpg" height="75" width="75" alt="Chinese Children"/></a>
...

Many thanks...


 
Old May 5th, 2006, 10:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Structures like this containing escaped markup are nasty to deal with. I don't know why people produce documents like that, when XML was designed to allow markup to be nested hierarchically. You've got a choice between trying to handle it "manually" within the XSLT code, using string manipulation functions such as substring and contains to manipulate the markup as text, or sending the whole description out to an XML (or HTML) parser and then processing the resulting tree.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 5th, 2006, 10:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Oh, another option I should have mentioned is the old disable-output-escaping hack - this is the one case where it's sometimes forgiveable to use it. If you've got escaped markup in your input, then copying it to the output with disable-output-escaping will cause it to appear in the output as real markup rather than escaped markup. But watch out, some environments such as Mozilla don't support d-o-e.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 5th, 2006, 10:28 AM
Registered User
 
Join Date: May 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael,

Yes. I hate that too :( why not standard?

I'm thinking of replace &gt; and &lt; in description node and it will work in html? how to replace the &gt; or &lt; in the xslt code? Pleae help.

<xsl:template match="/rdf:RDF">

    <xsl:for-each select="*[name() = 'item']"
           xmlns:rss="http://purl.org/rss/1.0/">

     <a href="{rss:link}" target="_main">
   <xsl:value-of select="rss:title"/>
    </a>
    <br></br>
    <xsl:value-of select="rss:description"/>
  </xsl:for-each>

</xsl:template>

</xsl:stylesheet>







Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Missing Node (Node data) pashworth XSLT 3 January 29th, 2007 12:39 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
document node order vs sort node order. ladyslipper98201 XSLT 2 June 5th, 2003 11:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.