I have searched the internet/and posts on here to try and find an answer to the question I have. I am learning XML and have an assignment I have to turn a previous .xml to .xslt. I have gotten all of it but a small matter of code that will not find the value I have inputed.
For example; and I know there are probably tons of other ways to make this happen but this is the code I have been given.
A part of the .xml is as follows:
<collection>
<movie>
<title>Braveheart</title>
<genre v_genre="Action"/>
<rating v_rating="R"/>
<grading v_grading="4"/>
<summary>William Wallace, a commoner, unites the 13th Century Scots in their battle to overthrow Englands rule </summary>
<year>1995</year>
<director>Mel Gibson</director>
<runtime>177</runtime>
<studio>Icon Entertainment International</studio>
<actors>
<actor ID="001">Mel Gibson</actor>
<actor ID="002">Sophie Marceau</actor>
<actor ID="003">Patrick McGoohan</actor>
</actors>
</movie>
Now for this I do not understand the value.
If someone could help me out with understand that part I would greatly appreciate it. Part 2 of the question is going to an .xslt doc all populates correctly except the following sectors:genre, rating, and grading. I have tried a multiple different ways to try and get the values to populate. Here is a section of the code.
<xslt:for-each select="collection/movie">
<tr>
<td>
<xslt:value-of select="title"/>
</td>
<td>
<xslt:value-of select="genre"/>
</td>
<td>
<xslt:value-of select="rating/v_rating"/>
</td>
<td>
<xslt:value-of select="grading/v_grading"/>
</td>
<td>
<xslt:value-of select="summary"/>
</td>
<td>
<xslt:value-of select="year"/>
</td>
<td>
<xslt:value-of select="director"/>
</td>
<td>
<xslt:value-of select="runtime"/>
</td>
<td>
<xslt:value-of select="studio"/>
</td>
<td>
<xslt:value-of select="actors"/>
</td>
</tr>
</xslt:for-each>
</table>
</body>
</html>
</xslt:template>
</xslt:stylesheet>
I am trying to actually understand why that is that way for future uses. The whole college thing. Hopefully this will not get deleted. I have searched tons of places to figure this out but no examples list it this way. Thank you in advanced.
The elements you are having trouble with are these
So, if you notice, the values you want are held in attributes, not child text nodes. Therefore you need to change your xsl:value-of to this (in the the example of genre
If you are keen to learn XSLT, it might be worth knowing it is preferable to use xsl:apply-templates over xsl:for-each. Additionally, you should look to remove repitition in you code. Looking at your example, the table cells are output in the same order as the child elements in your XML. Therefore, you could create a generic template to match most of the child elements to output table cells.
Try this XSLT
Note that the XSLT processor will match the more specific named element (e.g. genre) before matching the generic template for
*at the end.