开发者

XSL: selecting data from a node based on the value of a variable

I'm working on a minor project learning XSL, and I've run across a problem...

I have a Docbook file with a series of entires for people categorized by Department. Occasionally, however, I will have a person who works in a minor group as well as his own. To avoid duplication of data, I specified that a person node either contains the data in their node, or an xref node that links to their main node. When I iterate over all people in a group, I need to check whether the node is a linked node or a data node and adjust my variables accordingly.

Here's the choose code

<xsl:choose>
    <xsl:when test="xref">
        <xsl:variable name="TAG_ID" select="xref/@linkend" />
        <xsl:variable name="NAME" select="//*[@id='$TAG_ID']/para[@id='who']" />
        <xsl:variable name="EMAIL" select="//*[@id='$TAG_ID']/para[@id='who']/ulink/@url" />
        <xsl:variable name="IMAGE" select="//*[@id='$TAG_ID']/para[@id='image']" />
        <xsl:variable name="MEET" select="//*[@id='$TAG_ID']/para[@id='meet']" />
        <xsl:call-template name="output_person" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="NAME" select="para[@id ='who']" />
        <xsl:variable name="EMAIL" select="para[@id='who']/ulink/@url" />
        <xsl:variable name="IMAGE" select="para [@id='image']" />
        <xsl:variable name="MEET" select="para [@id='meet']" />
        <xsl:call-template name="output_person" />
    </xsl:otherwise>
</xsl:choose>

However, when I try to run this I get the following errors...

runtime error: file team.xsl line 92 element img
Variable 'IMAGE' has not been declared.
xmlXPathCompiledEval: evaluation failed

After some looking on the internet, I have simultaneously seen code that accomplishes this and people saying its impossible.

So my question is twofold.开发者_高级运维..

Can I select a specific node based on an variable?

and if not, can this even be done?


The variables you are declaring will not be in scope for your output_person template. In order for this to work, you need to have the output_person template accept params and then pass those params as part of call-template.

Also note that references to variables shouldn't be enclosed in quotes.

For example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- other templates -->
<xsl:template match="your-element">
    <xsl:choose>
        <xsl:when test="xref">
            <xsl:variable name="TAG_ID" select="xref/@linkend" />
            <xsl:variable name="NAME" select="//*[@id=$TAG_ID]/para[@id='who']" />
            <xsl:variable name="EMAIL" select="//*[@id=$TAG_ID]/para[@id='who']/ulink/@url" />
            <xsl:variable name="IMAGE" select="//*[@id=$TAG_ID]/para[@id='image']" />
            <xsl:variable name="MEET" select="//*[@id=$TAG_ID]/para[@id='meet']" />
            <xsl:call-template name="output_person">
               <xsl:with-param name="NAME" select="$NAME"/> 
                <xsl:with-param name="EMAIL" select="$EMAIL"/> 
                <xsl:with-param name="IMAGE" select="$IMAGE"/> 
                <xsl:with-param name="MEET" select="$MEET"/> 
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:variable name="NAME" select="para[@id ='who']" />
            <xsl:variable name="EMAIL" select="para[@id='who']/ulink/@url" />
            <xsl:variable name="IMAGE" select="para [@id='image']" />
            <xsl:variable name="MEET" select="para [@id='meet']" />
            <xsl:call-template name="output_person">
                <xsl:with-param name="NAME" select="$NAME"/> 
                <xsl:with-param name="EMAIL" select="$EMAIL"/> 
                <xsl:with-param name="IMAGE" select="$IMAGE"/> 
                <xsl:with-param name="MEET" select="$MEET"/> 
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template name="output_person">
    <xsl:param name="NAME"/>
    <xsl:param name="EMAIL"/>
    <xsl:param name="IMAGE"/>
    <xsl:param name="MEET"/>
    <!-- your logic here -->
</xsl:template>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜