Jump to content

How to select one element's value among...


jojo_slash
 Share

Recommended Posts

:) Here is my xml abstract:<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet type="text/xsl" href="xml_qtynote.xsl"?><note><subject></subject><topic> <tag1>topic </tag1> <tag2>title1: </tag2> <tag3>content1</tag3> <tag3>content2</tag3> <tag3>content3</tag3> <tag3>content4</desc> <tag3>content5</tag3> <tag2>title2:</tag2> <tag3>content1</tag3> <tag3>content2</tag3></topic></note>--------------------------------------------Here is my xsl abstract:<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><head><h2>My note</h2><xsl:apply-templates/></body></html></xsl:template><xsl:template match="subject"><p><xsl:apply-templates select="tag1"/><xsl:apply-templates select="tag2"/> <xsl:apply-templates select="tag3"/></p></xsl:template><xsl:template match="tag1">Topic: <xsl:value-of select="."/><br/></xsl:template><xsl:template match="tag2">Title: <span style="color:#ff0000"><xsl:value-of select="."/></span><br /></xsl:template><xsl:template match="tag3">.<span style="color:#00f000"><xsl:value-of select="."/></span><br/></xsl:template></xsl:stylesheet>----------------------------------------------I want to select content5 from tag3 element and make it shown as a hyperlink in html. I try it by assigning a attribute into the tag3 with content5 in the xml and use the conditional statement in xsl but found that doesn't work. Is it possible to do? How? I am just learning xml and xsl in the Tutorial and cannot find out the answer. Can someone help? Thanks! :)If you have any comments about the structure of my xml and xsl, please give advise. Thanks!
Link to comment
Share on other sites

I love the way your XSLT looks like. It almost makes me feel as all of my XSLT experiments were useless :) . In fact, that's the first time I see a working usage of the <xsl:apply-templates> where it comes as a really useful feature.Your XML could use a bit of recategorization though. You could group things that belong at one place. For example:

<tag4><tag2>title1: </tag2><contents><tag3>content1</tag3><tag3>content2</tag3><tag3>content3</tag3><tag3>content4</desc><tag3>content5</tag3></contents></tag4><tag4><tag2>title2:</tag2><contents><tag3>content1</tag3><tag3>content2</tag3></contents></tag4>

In this case, tag4 servs as a grouping for everything belongs to the particular title and contents and the contents element combines... well... yeah... all contents.Also I hope this is not the final version of the XML file you intent to use. Names such as "tag*" are horrible. Think of something with a meaning.--------------------------Anyway, let's get back to the problem. There are two ways of adding attributes in XSLT (which is practically what you need). I have described theese methods in detail here.

Link to comment
Share on other sites

First, thanks you for comments. I am using "tag" as name because I think it is better to illustrate on my question of interest. My final version will not be using that as the element's name. :) I had read the link you refer to before I post the question. But I am a slow learner and just start learning xml and xsl. I do not understand much the technical area(I did not know that post is related to my issue if you did not point out) :) .However, I still cannot solve the problem after trying to imitate the style of the code into my own. Here is the latest code:Here is my xml:<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet type="text/xsl" href="try.xsl"?><topic><tag4><tag2>title1: </tag2><contents><tag3>content1</tag3><tag3>content2</tag3><tag3>content3</tag3><tag3>content4</tag3><tag3>content5</tag3><tag5>C://Webdocs/java.html</tag5><tag6>content5</tag6></contents></tag4><tag4><tag2>title2:</tag2><contents><tag3>content1</tag3><tag3>content2</tag3></contents></tag4></topic>Here is my xsl:<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><h2>My note</h2><xsl:apply-templates/><a> <xsl:attribute name="href"> <xsl:value-of select="/contents/tag5" /> </xsl:attribute> <xsl:value-of select="/contents/tag6" /></a></body></html></xsl:template><xsl:template match="subject"><p><xsl:apply-templates select="tag1"/><xsl:apply-templates select="tag2"/> <xsl:apply-templates select="tag3"/></p></xsl:template><xsl:template match="tag1">Topic: <xsl:value-of select="."/><br/></xsl:template><xsl:template match="tag2">Title: <span style="color:#ff0000"><xsl:value-of select="."/></span><br /></xsl:template><xsl:template match="tag3">.<span style="color:#00f000"><xsl:value-of select="."/></span><br/></xsl:template></xsl:stylesheet>The result of that is it turn up the hyperlink and the word used for the link shown after content 5. This show that I do not understand the way the attribute is used. I think I am out of the track. Looking toward your reply. :(

Link to comment
Share on other sites

The way I see it, you were confused by your own structured way of presentig data :) . The only mistakes you have is that you misplaced the achor and it's content and thus you have wrong XPath expressions everywhere.Just to clear the things up with the link first: starting an XPath expression with a forward slash("/") makes it absolute. If you are going to use an absolute value, it should look like this:

<a><xsl:attribute name="href"><xsl:value-of select="/topic/contents/tag5" /></xsl:attribute><xsl:value-of select="/topic/contents/tag6" /></a>

Look at your XPath expressions. Adjustings to the XML always lead to adjustments in the XSLT. Now if I have understood your methods correctly, it should be something like this:

<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><h2>My note</h2><xsl:apply-templates/></body></html></xsl:template><xsl:template match="subject"><p><xsl:apply-templates select="tag1"/><xsl:apply-templates select="tag4"/></p></xsl:template><xsl:template match="tag1">Topic: <xsl:value-of select="."/><br/></xsl:template><xsl:template match="tag2">Title: <span style="color:#ff0000"><xsl:value-of select="."/></span><br /></xsl:template><xsl:template match="tag3"><xsl:choose><xsl:when test="tag5"><a><xsl:attribute name="href"><xsl:value-of select="tag5" /></xsl:attribute><xsl:value-of select="tag6" /></a><br/></xsl:when><xsl:otherwise><span style="color:#00f000"><xsl:value-of select="."/></span><br/></xsl:otherwise></xsl:choose></xsl:template><xsl:template match="tag4"><xsl:apply-templates select="tag2" /><xsl:apply-templates select="tag3 /></xsl:template></xsl:stylesheet>

And also some small adjustments to make the XML more organized again:

<tag3><tag5>C://Webdocs/java.html</tag5><tag6>content5</tag6></tag3>

Link to comment
Share on other sites

Thank you for the reply. :) I found that the structure of the xsl makes more sense than it was. I loaded the xml to see what I got and found that only the Topic and Title is shown only(the contents all missing). :blink: As I am not very clear about the difference between absolute and relative value(just know a bit more about absolute that exact path must be used), I try both way you gave me to see if I have been putting incorrect value into the XPath link but could not find out that way. Why would the contents become missing? :( (I really don't understand since after I read the structure you set up in my xsl I think it is perfectly make sense and correct). :) I posted my latest codes again: :) Here is my xml:<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet type="text/xsl" href="try.xsl"?><note><subject><tag1>topic1</tag1><tag4><tag2>title1: </tag2><contents><tag3>content1</tag3><tag3>content2</tag3><tag3>content3</tag3><tag3>content4</tag3><tag3>content5</tag3><tag3><tag5>C://Webdocs/java.html</tag5><tag6>content5</tag6></tag3></contents></tag4><tag4><tag2>title2:</tag2><contents><tag3>content1</tag3><tag3>content2</tag3></contents></tag4></subject></note>Here is my xsl:<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body><h2>My note</h2><xsl:apply-templates/></body></html></xsl:template><xsl:template match="subject"><p><xsl:apply-templates select="tag1"/><xsl:apply-templates select="tag4"/></p></xsl:template><xsl:template match="tag1">Topic: <xsl:value-of select="."/><br/></xsl:template><xsl:template match="tag2">Title: <span style="color:#ff0000"><xsl:value-of select="."/></span><br /></xsl:template><xsl:template match="tag3"><xsl:choose><xsl:when test="tag5"><a><xsl:attribute name="href"><xsl:value-of select="/topic/contents/tag5" /></xsl:attribute><xsl:value-of select="/topic/contents/tag6" /></a><br/></xsl:when><xsl:otherwise>Content: <span style="color:#00f000"><xsl:value-of select="."/></span><br/></xsl:otherwise></xsl:choose></xsl:template><xsl:template match="tag4"><xsl:apply-templates select="tag2" /><xsl:apply-templates select="tag3" /></xsl:template></xsl:stylesheet>Thank you for reading the codes again :( I see another question of interest is if I deleted the <xsl:template match="subject">...</xsl:template> there will be no difference on the outcome. Do I write up something that is not necessary in my xsl? :D

Link to comment
Share on other sites

<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body>	<h2>My note</h2>	<xsl:apply-templates select="note/subject"/></body></html></xsl:template>	<xsl:template match="note/subject">	<p>  <xsl:apply-templates select="tag1"/>  <xsl:apply-templates select="tag4"/>	</p>	</xsl:template>  <xsl:template match="tag1">  Topic: <xsl:value-of select="."/><br/>  </xsl:template>  <xsl:template match="tag4">  <xsl:for-each select=".">  	<xsl:apply-templates select="tag2" />  	<xsl:apply-templates select="contents/tag3" />  </xsl:for-each>  </xsl:template>  	<xsl:template match="tag2">  	Title: <span style="color:#ff0000">  	<xsl:value-of select="."/></span>  	<br />  	</xsl:template>  	<xsl:template match="contents/tag3">  	<xsl:for-each select=".">  	<xsl:choose>    <xsl:when test="tag5">    	<a>    	<xsl:attribute name="href">      <xsl:value-of select="tag5" />    	</xsl:attribute>    	<xsl:value-of select="tag6" />    	</a><br/>    </xsl:when>    <xsl:otherwise>    Content: <span style="color:#00f000">    <xsl:value-of select="."/>    </span>    <br/>    </xsl:otherwise>  	</xsl:choose>  	</xsl:for-each>  	</xsl:template></xsl:stylesheet>

I decided to take the code into my lab (Dreamwaver 8) for further investigation and this is what I got in the end. As I suspected, it's all wrong XPath expressions and nothing more. The XPath expression in the root ("/") template is optional btw. I put it just to ensure all is OK. I also added some "for-each"es, to ensure that not only the first record is shown :) .Btw, try to use the

 BBCode element to mark your code. It's easier to read and copy(for investigation) that way.
Link to comment
Share on other sites

Under the new codes you gave me, I found the outcome has improved. :) The contents has been shown. I think I can do some adjustment to make it the way I want now. Thank you for your hard work. :) Btw, to help you into the investigation, I put the tag that I have used in my codes as follow: :( tag 1= headtag 2= titletag 3= descriptiontag 4= blocktag 5= linktag 6= name_linkHope it is useful to you. :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...