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 <= $linknum])"/>
</xsl:when><xsltherwise>
<!-- position() reports non-"link" siblings: use count()
-->
<xsl:value-of
select="count(self::*/preceding-sibling::link)+1"/>
</xsltherwise></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><xsltherwise>
<a href="{$uselink}">
<xsl:value-of
select="ancestor::descrip/insertLink[position()=$linkpos]/name"/></a>
</xsltherwise></xsl:choose>
</xsl:template>
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 <= $linknum])"/>
</xsl:when><xsltherwise>
<!-- position() reports non-"link" siblings: use count()
-->
<xsl:value-of
select="count(self::*/preceding-sibling::link)+1"/>
</xsltherwise></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><xsltherwise>
<a href="{$uselink}">
<xsl:value-of
select="ancestor::descrip/insertLink[position()=$linkpos]/name"/></a>
</xsltherwise></xsl:choose>
</xsl:template>