Needing help with XSLT for-each using XPATH
I am working on a project that uses XSLT to transform an HTML string to extract some specific values. Can you please look at the HTML/XSLT below and see what it is that I am doing wrong?
BTW In the HTML below I need to extract value from <a>
which says "something" (XPATH = //div[@id='result']/div[2]/div[1]/a[1]):
<html>
<head></head>
<body>
<div>
<div id="whatever">
<!--other stuff-->
</div>
<div id="whatever2">
<!--other stuff-->
<div>
<div class='listtable'>
<div>
<div id="result">
<div class="ignore">
<!--other stuff-->
</div>
<div>
<div>
<a>something</a>
</div>
</div>
</div>
</div>
<div>
<div id="result">
<div class="ignore">
<!--other stuff-->
</div>
<div>
<div>
<a>something</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
so, the XSLT i came up with looks like this but it doesnt work ...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Link Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
</tr>
<xsl:for-each select="//div[@id='result']">
<tr>
<td><xsl:value-of select="/div[2]/div[1]/a[1]"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
P.S.: I have worked with XPATHS before but I have no experience with XSLT and for whatever reason Visual Studio crashes when I try to debug so I have no clue what went wrong here.
Thanks for your help!开发者_如何学JAVA
It is important to understand the difference between Relative and Absolute XPath expressions.
An absolute XPath expression starts with the sharacter '/'.
/div[2]/div[1]/a[1]
selects nothing if the top element of the XML document is not named div
On the other side, XPath expressions inside an <xsl:for-each>
or inside an <xsl:template>
most often are intended to select nodes off the current node. To do so, they must be relative.
A relative XPath expression never starts with '/'.
So, in your case you need to have:
div[2]/div[1]/a[1]
what it is that I am doing wrong?
It's just a tiny error you are committing. The XPath should be:
"div[2]/div[1]/a[1]"
In this way you correctly select the elements starting from the current context of the xsl:for-each
. In the way you have provided (with the forwardslash at the beginning of the XPath), you are trying to select the elements starting always from the root of the input document.
for whatever reason Visual Studio crashes when I try to debug so I have no clue what went wrong here.
If Visual Studio is crashing is probably because some flaw there is in it. May be you need some patch. The XSLT you have provided is correct and won't never going to crash with an XSLT processor. You should obtain a successful result also with the following identical transform:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Link Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
</tr>
<xsl:apply-templates select="//div[@id='result']"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="div[@id='result']">
<tr>
<td><xsl:value-of select="div[2]/div[1]/a[1]"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
精彩评论