Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Define a variable with variables

Reply
Thread Tools

Define a variable with variables

 
 
axial@axialinfo.com
Guest
Posts: n/a
 
      04-19-2005
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

 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      04-20-2005
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
 
Reply With Quote
 
 
 
 
axial@axialinfo.com
Guest
Posts: n/a
 
      04-20-2005
>> 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 >

 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      04-20-2005

> 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">
<xslaram name="thischap"/>
<xsl:value-of select="$thischap/*[name()=current()]"/>
</xsl:template>


David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57