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 February 23rd, 2007, 04:34 AM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to use Xpath to extract child nodes

Hi,

I need to extract the value of the Document_Body_content from below xhtml content. I used something similar to

String xpq="//div[div/div/div/h2/a[@name='Document_Body_content']]/self::div";

I tried like this but its not returning me the exact output....

My Sample xhtml :

-------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<title></title>
</head>
<body>
<div class="article" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a id="ID" name="ID"></a></h2>
</div>
</div>


</div>

<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="Description_content"
name="Description_content"></a> <span
class="underline">Description</span></h2>
</div>
</div>
</div>
</div>

<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="Document_Body_content"
name="Document_Body_content"></a> <span class="underline">Document
Body</span></h2>
</div>
</div>
</div>

<p>To check the mount options on your NFS mounted filesystems, use
the <span class="italic">nfsstat -m</span> command. This will
display all the current settings.</p>

<p></p>

<p><span class="italic">Example:</span></p>

<p><span class="italic">% nfsstat -m</span></p>

<p><span class="italic">/home/&lt;username&gt; from
&lt;NFS_Server&gt;:/global/export/home1/&lt;username&gt; Flags:
vers=3,proto=tcp,sec=sys,hard,intr,link,symlink,ac l,rsize=32768,wsize=32768,retrans=5</span></p>

<p><span class="italic">/usr/dist from &lt;NFS_Server&gt;:/usr/dist
Flags:
vers=3,proto=tcp,sec=sys,soft,intr,link,symlink,ac l,rsize=32768,wsize=32768,retrans=5</span></p>

<p><span class="italic">/net/share/ from
&lt;NFS_Server&gt;:/share/export Flags:
vers=3,proto=tcp,sec=sys,hard,intr,link,symlink,ac l,rsize=32768,wsize=32768,retrans=5</span></p>

<p><span class="bold"><b>If any of the mount options are changed,
you must umount and remount the filesystem for the changes to take
effect. Only then will the changes be displayed with the <span
class="italic">nfsstat -m</span> command.</b></span></p>

<p>Note: the &lt;username&gt; and &lt;NFS_Server&gt; are
"placeholders" which represent the actual user and server names. Of
course, the server noted in each mount point could easily be
different (or not).</p>

<p></p>
</div>

<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a
id="_________Internal_Only_______content"
name="_________Internal_Only_______content"></a> <span
class="underline">Internal Only</span></h2>
</div>
</div>
</div>

<p>[email protected]</p>

<p></p>
</div>
</div>
</body>
</html>

For Document_Body_content, I expect my output as below

<p>To check the mount options on your NFS mounted filesystems, use
the <span class="italic">nfsstat -m</span> command. This will
display all the current settings.</p>

<p></p>

<p><span class="italic">Example:</span></p>

<p><span class="italic">% nfsstat -m</span></p>

<p><span class="italic">/home/&lt;username&gt; from
&lt;NFS_Server&gt;:/global/export/home1/&lt;username&gt; Flags:
vers=3,proto=tcp,sec=sys,hard,intr,link,symlink,ac l,rsize=32768,wsize=32768,retrans=5</span></p>

<p><span class="italic">/usr/dist from &lt;NFS_Server&gt;:/usr/dist
Flags:
vers=3,proto=tcp,sec=sys,soft,intr,link,symlink,ac l,rsize=32768,wsize=32768,retrans=5</span></p>

<p><span class="italic">/net/share/ from
&lt;NFS_Server&gt;:/share/export Flags:
vers=3,proto=tcp,sec=sys,hard,intr,link,symlink,ac l,rsize=32768,wsize=32768,retrans=5</span></p>

<p><span class="bold"><b>If any of the mount options are changed,
you must umount and remount the filesystem for the changes to take
effect. Only then will the changes be displayed with the <span
class="italic">nfsstat -m</span> command.</b></span></p>

<p>Note: the &lt;username&gt; and &lt;NFS_Server&gt; are
"placeholders" which represent the actual user and server names. Of
course, the server noted in each mount point could easily be
different (or not).</p>

<p></p>


Please let me know how to incorporate xpath for this scenario.

Thanks
Suresh

 
Old February 23rd, 2007, 05:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

All your input elements are in the XHTML namespace, so in a path expression the names need to be prefixed, for example

<xsl:copy-of select="h:dv/h:div[h:a='...']">

with xmlns:h="http://www.w3.org/1999/xhtml"

declared at the top level of your stylesheet.

I would use something like

<xsl:copy-of select="//h:div[@class='section']
   [.//h:a/@id='Document_Body_content']/h:p"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 23rd, 2007, 06:04 AM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael,

Thanks a lot for your immediate response...

I am using this xpath with java...I m passing this xpath to a XpathFactory but still its not returning me required output..

            DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
            Document dom = domBuilder.parse(new InputSource(new FileReader(args[0])));

            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPath xpath = xpathFactory.newXPath();
            String xpq = "//h:div[@class='section'][.//h:a/@id='Document_Body_content']/h:p";
            Node node = (Node) xpath.evaluate(xpq, dom, XPathConstants.NODE);
            transformer.transform(new DOMSource(node), new StreamResult(System.out));

So could you please let me know how to directly incorporate xpath query in this case ?

Sorry for pasting my whole xhtml content
--------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<xsl:copy-of select="h:div/h:div[h:a='...']"></xsl:copy-of>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org" />
<title></title>
</head>
<body>
<div class="article" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a id="ID" name="ID"></a></h2>
</div>
</div>


</div>

<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="Description_content"
name="Description_content"></a> <span
class="underline">Description</span></h2>
</div>
</div>
</div>
</div>

<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="Document_Body_content"
name="Document_Body_content"></a> <span class="underline">Document
Body</span></h2>
</div>
</div>
</div>

<p><span class="bold"><b>How to have a secured nfs without nis or
nis+.</b></span></p>

<p><span class="bold"><b>On Server :</b></span></p>

<p>1. Make sure that the /etc/publickey does not have keys other
than <span class="italic">nobody</span>.</p>

<p>2. vi /etc/defaultdomain (needed for netname)</p>

<p>Enter any domain name</p>

<p>reboot</p>

<p>3. newkey -h &lt;machinename which is servername&gt; -s
files</p>

<p>Adding new key for
unix.&lt;machinename&gt;@&lt;domainname&gt;</p>

<p>Enter local root login password: (enter root passwd for that
machine)</p>

<p>Enter &lt;machinename's&gt; root login password: (enter rpc
passwd you want)</p>

<p>4. newkey -h &lt;machinename which are clients&gt; -s files</p>

<p><span class="italic">Same procedure as 3</span></p>

<p>5. keylogin -r</p>

<p>Enter rpc passwd.</p>

<p>Wrote secret key in .rootkey</p>

<p>6. vi /etc/dfs/dfstab</p>

<p>share -F nfs -o sec=dh /&lt;filesystem to be shared&gt;</p>

<p>shareall</p>

<p></p>

<p><span class="bold"><b>On Client</b></span>:</p>

<p>1. vi /etc/defaultdomain (needed for netname)</p>

<p>Enter the same domainname as the server</p>

<p>reboot</p>

<p>2. copy file /etc/publickey from server to the client.</p>

<p>3. Make sure that /etc/publickey on server and client are the
same.</p>

<p>4. keylogin -r</p>

<p>Enter the rpc passwd for client.</p>

<p>5. mount -F nfs -o sec=dh &lt;server&gt;:/&lt;filesystem it is
sharing&gt; &lt;mount point&gt;</p>

<p>You should be able to mount .</p>
</div>

<div class="section" lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a
id="_________Internal_Only_______content"
name="_________Internal_Only_______content"></a> <span
class="underline">Internal Only</span></h2>
</div>
</div>
</div>

<p>None.</p>
</div>
</div>
</body>
</html>
------------------------------------------------------------





 
Old February 23rd, 2007, 06:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to set the namespace mapping in the XPath object.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
extract attribute value xPath chetrity XML 2 April 4th, 2011 07:09 PM
text and child nodes eepyoga XSLT 1 April 8th, 2008 12:45 PM
XPATH removing nodes from file?? dparsons ASP.NET 2.0 Professional 1 February 27th, 2007 05:37 PM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM
XPath not finding nodes, need help Alderian72 XSLT 1 June 16th, 2005 10:49 AM





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