Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Define a variable with variables (http://www.velocityreviews.com/forums/t169180-define-a-variable-with-variables.html)

axial@axialinfo.com 04-19-2005 11:28 PM

Define a variable with variables
 
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


David Carlisle 04-20-2005 12:27 AM

Re: Define a variable with variables
 
axial@axialinfo.com writes:

> 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

axial@axialinfo.com 04-20-2005 02:10 AM

Re: Define a variable with variables
 
>> 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 ;-) >


David Carlisle 04-20-2005 09:37 AM

Re: Define a variable with variables
 

> 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


All times are GMT. The time now is 05:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.