Topic: xslt,xpath problems Pages that link to <a href="https://ozoneasylum.com/backlink?for=26232" title="Pages that link to Topic: xslt,xpath problems" rel="nofollow" >Topic: xslt,xpath problems\

 
Author Thread
beryl_peng
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Jul 2005

IP logged posted posted 07-13-2005 16:28 Edit Quote

dear all,


i am doing my project which need to convert xhtml to xml using xpath.
take for example, i need to collect the data from this td tag

code:
-<tr>
- <td align="left" valign="top">
  <img height="1" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="10" /> 
  13-Jul-2005 13:26:47 IST 
  </td>
  </tr>



i used to like using

code:
tr[1]/td/img/text()


xpath expression to collect the data i want
but my problems is every webpage may contain different number of total of row, so without knowing the x number of row, i cannot specify in the tr[x],
is there any other way to do it?

any help is greatly appreciated

thank u

(Edited by bitdamaged on 07-13-2005 17:26)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

IP logged posted posted 07-13-2005 17:30 Edit Quote

Sorry just cleaned up the path using square brackets like that can break in the board


Um is there something unique to that row? Without some form of identifier it's going to be hard to pull a specific row



.:[ Never resist a perfect moment ]:.

beryl_peng
Obsessive-Compulsive (I) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-13-2005 17:53 Edit Quote

hi hi,

thanks for reply.
cause i am thinking of using one xsl stylesheet to extract my data
is it possible to do like a for loop then increment the variable i

like this

code:
tr[i]/td/img/text()



thanks

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

IP logged posted posted 07-13-2005 19:34 Edit Quote

If you are using XSLT why not use XSLT, that is what the <xsl:for-each/> loop or the <xsl:apply-templates/> is for.

If you put you XSLT in here I might be able to offer you further assistance in getting done what you need to get done, but the XPath without anything else is not going to give me enough to work with.

Dan @ Code Town

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-13-2005 20:19 Edit Quote

hi hi,

thanks for reply.

code:
- <tr>
- <td bgcolor="#ffffff" colspan="10" height="2">
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="100%" /> 
  </td>
  </tr>
- <tr bgcolor="#ffffff">
- <td height="20" valign="middle" width="10">
  <img alt="" height="20" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="10" /> 
  </td>
- <td align="left">
  <a href="http://contact.ebay.in/ws/eBayISAPI.dll?ReturnUserEmail&requested=naveen71&iid=8316776438&frm=992">naveen71</a> 
  <img border="0" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="4" /> 
  ( 
  <a href="http://feedback.ebay.in/ws/eBayISAPI.dll?ViewFeedback&userid=naveen71">2</a> 
  ) 
  </td>
  <td align="middle" nowrap="nowrap" valign="top">Rs. 349.00</td> 
  <td align="middle" valign="top">1</td> 
- <td align="left" valign="top">
  <img height="1" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="10" /> 
  06-Jul-2005 18:04:26 IST 
  </td>
- <td>
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="1" /> 
  </td>
  </tr>
- <tr>
- <td bgcolor="#f9f9f9" colspan="10" height="2">
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="100%" /> 
  </td>
  </tr>
- <tr bgcolor="#f9f9f9">
- <td height="20" valign="middle" width="10">
  <img alt="" height="20" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="10" /> 
  </td>
- <td align="left">
  <a href="http://contact.ebay.in/ws/eBayISAPI.dll?ReturnUserEmail&requested=sweetsam84&iid=8316776438&frm=992">sweetsam84</a> 
  <img border="0" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="4" /> 
  ( 
  <a href="http://feedback.ebay.in/ws/eBayISAPI.dll?ViewFeedback&userid=sweetsam84">0</a> 
  ) 
  </td>
  <td align="middle" nowrap="nowrap" valign="top">Rs. 349.00</td> 
  <td align="middle" valign="top">1</td> 
- <td align="left" valign="top">
  <img height="1" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="10" /> 
  07-Jul-2005 09:42:50 IST 
  </td>
- <td>
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="1" /> 
  </td>
  </tr>




i am totally new to xslt and
i try with this xslt


code:
<xsl:template match="table[starts-with(normalize-space(tr/td[2]/span/b/text()),'User ID:')]">
		<xsl:for-each select="tr[$counter+1]">
		<buyer>
		<xsl:value-of select="td[2]/a/text()"/>
		</buyer>
		<price>
		<xsl:value-of select="td[3]/text()"/>
		</price>
		</xsl:for-each>
	</xsl:template>



but it only can get the first tr result, but what i want is after the for loop can get

code:
tr[1] , tr[2]



hope you understand what i mean :>

thank u

(Edited by beryl_peng on 07-13-2005 20:20)

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

IP logged posted posted 07-13-2005 23:46 Edit Quote

Your for-each looks fine if you lose the square braces, you then should be fine to match against every <tr>.

The match on your template looks really funky. I don't know how you are expecting to match XPath in the text of a bold tag in a span. If you are the HTML must be pretty crazy.

Dan @ Code Town

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 04:22 Edit Quote

hi hi

this is the starting point where i match my xhtml

code:
- <table border="0" cellpadding="0" cellspacing="0" width="100%">
- <tr bgcolor="#efefef">
- <td valign="middle" width="10">
  <img alt="" height="20" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="10" /> 
  </td>
- <td nowrap="nowrap" valign="middle" width="35%">
- <span class="standarditle">
  <b>User ID:</b> 
  </span>
  </td>
  <td align="center" class="standardtitle" nowrap="nowrap" valign="middle" width="25%">Price</td> 
  <td align="center" class="standardtitle" valign="middle" width="5%">Qty</td> 
  <td align="left" bgcolor="#cccccc" class="standardtitle" nowrap="nowrap" valign="middle" width="35%">Date of Purchase</td> 
- <td>
  <img alt="" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="10" /> 
  </td>
  </tr>
- <tr>
- <td bgcolor="#ffffff" colspan="10" height="2">
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="100%" /> 
  </td>
  </tr>
- <tr bgcolor="#ffffff">
- <td height="20" valign="middle" width="10">
  <img alt="" height="20" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="10" /> 
  </td>
- <td align="left">
  <a href="http://contact.ebay.in/ws/eBayISAPI.dll?ReturnUserEmail&requested=naveen71&iid=8316776438&frm=992">naveen71</a> 
  <img border="0" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="4" /> 
  ( 
  <a href="http://feedback.ebay.in/ws/eBayISAPI.dll?ViewFeedback&userid=naveen71">2</a> 
  ) 
  </td>
  <td align="middle" nowrap="nowrap" valign="top">Rs. 349.00</td> 
  <td align="middle" valign="top">1</td> 
- <td align="left" valign="top">
  <img height="1" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="10" /> 
  06-Jul-2005 18:04:26 IST 
  </td>
- <td>
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="1" /> 
  </td>
  </tr>
- <tr>
- <td bgcolor="#f9f9f9" colspan="10" height="2">
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="100%" /> 
  </td>
  </tr>
- <tr bgcolor="#f9f9f9">
- <td height="20" valign="middle" width="10">
  <img alt="" height="20" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="10" /> 
  </td>
- <td align="left">
  <a href="http://contact.ebay.in/ws/eBayISAPI.dll?ReturnUserEmail&requested=sweetsam84&iid=8316776438&frm=992">sweetsam84</a> 
  <img border="0" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="4" /> 
  ( 
  <a href="http://feedback.ebay.in/ws/eBayISAPI.dll?ViewFeedback&userid=sweetsam84">0</a> 
  ) 
  </td>
  <td align="middle" nowrap="nowrap" valign="top">Rs. 349.00</td> 
  <td align="middle" valign="top">1</td> 
- <td align="left" valign="top">
  <img height="1" src="http://pics.ebaystatic.com/aw/pics/s.gif" width="10" /> 
  07-Jul-2005 09:42:50 IST 
  </td>
- <td>
  <img alt="" height="1" src="http://pics.ebaystatic.com/aw/pics/x.gif" title="" width="1" /> 
  </td>
  </tr>
  </table>



i will try now


thanks
[edit by Skaarjj: fixing UBB]
(Edited by beryl_peng on 07-14-2005 04:55)

(Edited by Skaarjj on 07-15-2005 13:00)

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 08:26 Edit Quote

hi,


i sucessfully get the data, but is it possible to dynamically name the element tag name.

what i get:

code:
<buyer>
naveen71
</buyer>
<buyer>
sweetsam84
</buyer>



is it possible to make it like

code:
<buyer_$counter>
naveen71
</buyer>
<buyer_$counter+1>
sweetsam84
</buyer>



i try with this but get compiled error.
is there any way to name the tag by using variable?

thanks

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

IP logged posted posted 07-14-2005 15:41 Edit Quote

You might be able to key off of the TR count.

<xsl:element/>

Dan @ Code Town

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 18:33 Edit Quote

hi,

thanks for your suggestion. i try with this.

code:
<xsl:param name="counter" select="'2'"/>
........
..........
<xsl:template match="table[starts-with(normalize-space(tr/td[2]/span/b/text()),'User ID:')]">
		<xsl:for-each select="tr">
		
		<xsl:element name="$buyer">
		<xsl:value-of select="td[2]/a/text()"/>
                                <xsl:value-of select="$counter+1"/>
		</xsl:element>
</xsl:template>




but how come i fail to get the tag name printed out.
what i want is,

code:
<2>
value
3
</2>



and can i know whether the counter will keep track of the previous value in the for loop or always follow the default value as i always get the value for $counter+ 1 is 3,
i thought that it should be 3,4,5.....

thanks !!

beryl

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

IP logged posted posted 07-14-2005 18:55 Edit Quote

I would do the following.

code:
<xsl:template match="your crazy table match">
  <xsl:for-each select="tr">
    <buyer>
      <name><xsl:value-of select="select the name"/></name>
      <number><xsl:value-of select="position()"/></number>
    </buyer>
  </xsl:for-each>
</xsl:template>



I am not testing this. It is a general idea, but it might fail.

Dan @ Code Town

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

IP logged posted posted 07-14-2005 18:59 Edit Quote
quote:

what i want is,
<2>
value
3
</2>


Element names cannot start with a digit.

quote:

and can i know whether the counter will keep track of the previous
value in the for loop or always follow the default value as i always
get the value for $counter+ 1 is 3,
i thought that it should be 3,4,5.....


Use

code:
$counter+position()

or just

code:
position()

as WarMage said.

(Edited by HZR on 07-14-2005 19:02)

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 19:16 Edit Quote

hi,

thanks for helping.
but for my data collection, i need to collect many buyer details.
so what i hope to have is like this:

code:
<buyer_name1>naveen71</buyer_name1>
<buyer_name2>sweetsam84</buyer_name2>



and i hope to generate the tag name dynamically, is it possible?

again, thanks for all of your helps.

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

IP logged posted posted 07-14-2005 19:36 Edit Quote
code:
<xsl:element name="buyer_name{position()}">[element content]</xsl:element>


However, it would probably be better with a structure something like

code:
<buyer id="1">...</buyer>
<buyer id="2">...</buyer>



(Edited by HZR on 07-14-2005 19:38)

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 20:02 Edit Quote

hi hi

thanks a lot~~~~~~
another problems here, for my for loop, i will get some unwanted value
and i try to use the <xsl:if> to help me

take for example, the first tr/td value is "a", but i want to disgard this
so i try with this

code:
<xsl:template match="your crazy table match">
  <xsl:for-each select="tr">
    <xsl:if test="name != a">
    <buyer>
      <name><xsl:value-of select="select the name"/></name>
      <number><xsl:value-of select="position()"/></number>
    </buyer>
   </xsl:if>
  </xsl:for-each>
</xsl:template>



my intention is wan to eliminate the buyer name 'a' , but what i got is all buyer name is eliminate and fail to get the buyer details.


thanks a lot!

beryl

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

IP logged posted posted 07-14-2005 20:18 Edit Quote
quote:
take for example, the first tr/td value is "a"


Then it probably should be

code:
<xsl:if test="td != a">

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 20:45 Edit Quote

hi,

thanks , but still cannot.

code:
<table>
<tr>
<td>A</td>
</tr>
<tr>
<td>B</td>
</tr>
</table>



i want to discard the "A"

so this is how my xsl code

code:
<xsl:for-each select="tr">
<xsl:if test="tdtext() != A">
		<buyer_name>
		<xsl:value-of select="td/text()"/>
		</buyer_name>
 </xsl:if>
</xsl:for-each>



so i should be able to get "B". but instead, i didnt get anything
it seem not it stop at the if statement there

thanks!!!!

(Edited by beryl_peng on 07-14-2005 20:47)

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

IP logged posted posted 07-14-2005 20:52 Edit Quote

I made an error up there. Use:

code:
<xsl:if test="td/text() != 'A'">

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-14-2005 21:05 Edit Quote

hi hi,


THANKS so much....

cheers,

beryl

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-15-2005 11:09 Edit Quote

hi hi,

another doubts here.

can i know how to compare a string which contain '
take for example:

code:
<xsl:if test="td/text() != ' Cashier's Check '">



there is a ' inside the compared string.

thanks in advance !

HZR
Paranoid (IV) Inmate

From: Cold Sweden
Insane since: Jul 2002

IP logged posted posted 07-15-2005 15:28 Edit Quote
code:
<xsl:if test='td/text() != " Cashier&apos;s Check "'>

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-15-2005 16:41 Edit Quote

hi hi, thanks so much

can i know whether xslt provide substring function?
take for example:


code:
- <tr>
- <td>
  <p class="plabelb">Shipping fee: $1.00</p> 
  </td>
  </tr>
- <tr>
- <td>
  <p class="plabelb">The seller has chosen the following shipping services:</p> 
  </td>
  </tr>



and i just want to compare the "Shipping fee" string
take for example:

substring( tr/td/p/text() ) = 'Shipping fee'
as the full string for tr/td/p/text() is Shipping fee: $1.00


thanks so much!

Nathus
Bipolar (III) Inmate

From: Minnesota
Insane since: Aug 2003

IP logged posted posted 07-15-2005 17:01 Edit Quote

XSLT has a substring function, but what I think you're looking for is the contains function.

Some links on them: http://www.w3schools.com/xpath/xpath_functions.asp
http://builder.com.com/5100-6389-1054414.html

beryl_peng
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2005

IP logged posted posted 07-17-2005 19:40 Edit Quote

hi there,

can i know is it possible to do like this:

code:
<xsl:if test="position() = '6' ">
	 <xsl:if test = "TD/P/text() = 'Time Left:'">
	  <xsl:variable name="n" select=" 'A' "/>
         </xsl:if>
        </xsl:if>

<xsl:if test="position() = '8' ">
	 <xsl:if test = "TD/P/text() = 'Time Left:'">
	  <xsl:variable name="n" select=" 'B' "/>
         </xsl:if>
        </xsl:if>



and have another xsl: if to check the value of n?

thanks



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu