Define a variable with variables

A

axial

Is there a way to define a variable by resolving other variables?

I'm trying to build an "uber-variable" from user-selected components. I
have control over the XML and can change it as needed.

The user is allowed to specify one to three data fields from a list and
up to three text fields to use as the pattern for creating chapter
names:

<ChapterNameDefaults>
<Data>PartCode</Data>
<Text> </Text>
<Data>SectionNumber</Data>
<Text>.</Text>
<Data>ChapterNumber</Data>
</ChapterNameDefaults>

I need to create a "dynamically concatenated variable" that would
result if something like this was possible:

<xsl:variable name="ChapterName" select="{$PartCode}{
}{$SectionNumber}{.}{$ChapterNumber}"/>

or

<xsl:variable name="ChapterName" >
<xsl:value-of select="$PartCode"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$SectionNumber"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$ChapterNumber"/>
</xsl:variable>

which would be dynamically resolved to something like "ADR 0.1" or
"Billing 24-4" (in this example) depending on the user's selection of
variables, their resolved values, and Text.

Of course I could output a resolved ChapterName in the XML to start
with, rather than at the XSL stage, but I'd rather do it in the XSL if
possible.

Suggestions appreciated.
Thx
 
D

David Carlisle

Is there a way to define a variable by resolving other variables?

yes (I think, it depends what you mean)
I'm trying to build an "uber-variable" from user-selected components. I
have control over the XML and can change it as needed.

The user is allowed to specify one to three data fields from a list and
up to three text fields to use as the pattern for creating chapter
names:

<ChapterNameDefaults>
<Data>PartCode</Data>
<Text> </Text>
<Data>SectionNumber</Data>
<Text>.</Text>
<Data>ChapterNumber</Data>
</ChapterNameDefaults>

I need to create a "dynamically concatenated variable" that would
result if something like this was possible:

<xsl:variable name="ChapterName" select="{$PartCode}{
}{$SectionNumber}{.}{$ChapterNumber}"/>

select takes an XPath expression and Xpath expressions never use {}
which are not special characters to Xpath, they may only appear in
strings. (XSLT uses them in attribute value templates to surround an
Xpath, but that's a different context)
You could use

select="concat($PartCode, ' ',$SectionNumber,'.',$ChapterNumber)"

or

<xsl:variable name="ChapterName" >
<xsl:value-of select="$PartCode"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$SectionNumber"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$ChapterNumber"/>
</xsl:variable>


The above is perfectly legal. Perhaps your question isn't really about
the concatenation but on how to define $PartCode and friends?
which would be dynamically resolved to something like "ADR 0.1" or
"Billing 24-4" (in this example) depending on the user's selection of
variables, their resolved values, and Text.

Of course I could output a resolved ChapterName in the XML to start
with, rather than at the XSL stage, but I'd rather do it in the XSL if
possible.

Suggestions appreciated.
Thx

David
 
A

axial

The above is perfectly legal. Perhaps your question isn't really
about
the concatenation but on how to define $PartCode and friends?

Yes, you're right, that's where I can't quite make the leap, how to
get from

<Data>PartCode</Data>

to specifying it as a variable name and then resolving it.

=====
<a carlisle across the pond ;-) >
 
D

David Carlisle

that's where I can't quite make the leap, how to
get from

<Data>PartCode</Data>

to specifying it as a variable name and then resolving it.


You wouldn't do that, variable refernces are compile time constructs
(just as they are in other languages such as C for example) so they
never depend on any run time information.

Your original post wasn't very clear what the input looked like.

You posted this:


<ChapterNameDefaults>
<Data>PartCode</Data>
<Text> </Text>
<Data>SectionNumber</Data>
<Text>.</Text>
<Data>ChapterNumber</Data>
</ChapterNameDefaults>

which I take it you want to be a template for the output but what is the
actual data like? I'll make a guess:

<Chapter>
<ChapterNumber>2</ChapterNumber>
<SectionNumber>3</SectionNumber>
<PartCode>1</PartCode>
<stuff>...</stuff>
</Chapter>


in which case

<xsl:template match="Chapter">
<xsl:variable name="ChapterName" >
<xsl:apply-templates select="/path/to/ChapterNameDefaults/*">
<xsl:with-param name="thischap" select"."/>
</xsl:apply-templates>
</xsl:variable>

..... <xsl:value-of select="$ChapterName"/>
</xsl:template>

<xsl:template match="ChapterNameDefaults/Data">
<xsl:param name="thischap"/>
<xsl:value-of select="$thischap/*[name()=current()]"/>
</xsl:template>


David
 

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,999
Messages
2,570,246
Members
46,841
Latest member
WilmerBelg

Latest Threads

Top