Question? Better Way?

M

Mark Johnson

I wanted a shorthand for adding external links to a document. So I
markup the document, by hand, with "<link>" elements. And when I push
a button, it is combined with other things to produce an XML. And then
an XSLT is applied to that to produce HTML output. Let's say I have as
that first stage XML:

<a1>
<descrip>
<blockquote>
Who'll
<em>
stop the rain?
<linebreak/>
Performed by:
<link/>
</em>
<linebreak/>
reference [
<link n="20"/>
,
<link>link3b</link>
,
<link n="10">backto1</link>
].
</blockquote>
<insertLink>
<name>link1</name>
<url>http://link1.com</url>
<num>10</num>
</insertLink>
<insertLink>
<name>link2</name>
<url>http://link2.com</url>
<num>20</num>
</insertLink>
<insertLink>
<name>link3</name>
<url>http://link3.com</url>
<num>25</num>
</insertLink>
</descrip>
</a1>


And have it come out from the XSLT:

Who'll <em>stop the rain?
<br> <br>
Performed by: <a href="http://link1.com">link1</a></em>
<br> <br>
reference [<a href="http://link2.com">link2</a>,<a
href="http://link3.com">link3b</a>,<a
href="http://link1.com">backto1</a>].


In other words, for each "descrip" node, read out the insertLink info
either in list order, or by "num" key, and then just discard the
insertLink's. If the link has text, use that. Otherwise, use the
"name" from the link as the text for that link.

I wonder if I'm missing something, and that there might be a better
way to proceed, either with the design, above, or the XSLT, below:


<xsl:template match="link[ancestor::descrip/insertLink]">

<xsl:variable name="linkpos">
<xsl:choose><xsl:when test="@n">
<xsl:variable name="linknum" select="@n"/>
<xsl:value-of
select="count(ancestor::descrip/insertLink[./num &lt;= $linknum])"/>
</xsl:when><xsl:eek:therwise>
<!-- position() reports non-"link" siblings: use count()
-->
<xsl:value-of
select="count(self::*/preceding-sibling::link)+1"/>
</xsl:eek:therwise></xsl:choose>
</xsl:variable>
<xsl:variable name="uselink"
select="ancestor::descrip/insertLink[position()=$linkpos]/url"/>

<xsl:choose>
<xsl:when test="text()">
<a href="{$uselink}"><xsl:value-of select="text()"/></a>
</xsl:when><xsl:eek:therwise>
<a href="{$uselink}">
<xsl:value-of
select="ancestor::descrip/insertLink[position()=$linkpos]/name"/></a>
</xsl:eek:therwise></xsl:choose>

</xsl:template>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top