开发者

Display repeated tags in XSLT?

开发者 https://www.devze.com 2023-04-12 05:08 出处:网络
I am working with a CV project using XML for storing CVs, using XSLT and Java to transform it to HTML.

I am working with a CV project using XML for storing CVs, using XSLT and Java to transform it to HTML.

Following is the format of XML file

<CVs>
<cv>
<name>...</name>
<dob>...</dob>
<experience>
<job>.....</job>
<job>.....</job>
</experience>
</cv>
<cv>
<name>...</name>
<dob>...</dob>
<experience>
<job>.....</job>
<job>.....</job>
<job>.....</job>
<job>.....</job>
<job>.....</job>
</experience>
</cv>
</CVs>

What is the right way to display the <job> repeatedly? I am getting only one <job> and that the first one...

Have a nice day John


Thanks a lot for your reply. :)

I tried the first solution. It works. But what I what I want is to display all the with a HTML paragraph formatting.

First display the No.1, then there should be a paragraph, then No.2 and so on. like-

<p>Job No1<开发者_如何转开发/p>
<p>Job No2</p>

I used following - with a failure to get the required result..

<xsl:for-each select="Job">
<p style="color:red">
<xsl:if test="position() &gt; 1">,</xsl:if>
<xsl:value-of select="."/>
</p>
</xsl:for-each> 

Using the above code I am getting output like -

<p>Job No1 job No2..</p>

I am using XSLT 1.0, which I mention to forget in the first post. Sorry for that, but you judged it.. Hmmm vast experience with XSLT.. Great..

Can you help me again..?

Have a nice day John


LarsH - Right said, I should have done all this in the first post itself, but somehow can't manage it. Now I have practiced the posting techniques and hereafter will do it correctly. :)

Also, I have accepted Martin's answer. It did the job, with little modification I got the answer for the 2nd post of mine.

Thanks a lot to all of you...

Have a nice day John


I guess the problem is that with XSLT 1.0 (or with an XSLT 2.0 processor running an XSLT 1.0 stylesheet in backwards compatible mode) the code

<xsl:value-of select="job"/>

outputs the string value of the first job element. If you want to output the value of all job elements then with XSLT 1.0 you need to use either

<xsl:for-each select="job">
  <xsl:if test="position() &gt; 1">, </xsl:if>
  <xsl:value-of select="."/>
</xsl:for-each>

or <xsl:apply-templates select="job"/> with a template for job elements e.g.

<xsl:template match="job">
  <xsl:if test="position() &gt; 1">, </xsl:if>
  <xsl:value-of select="."/>
<xsl:template>

while with XSLT 2.0 all you need is <xsl:value-of select="job" separator=", "/>.

0

精彩评论

暂无评论...
验证码 换一张
取 消