Define a variable with variables

Discussion in 'XML' started by axial@axialinfo.com, Apr 20, 2005.

  1. Guest

    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
    , Apr 20, 2005
    #1
    1. Advertising

  2. 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
    David Carlisle, Apr 20, 2005
    #2
    1. Advertising

  3. Guest

    >> 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 ;-) >
    , Apr 20, 2005
    #3

  4. > 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
    David Carlisle, Apr 20, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David T. Ashley
    Replies:
    6
    Views:
    520
    Tim Rentsch
    Dec 5, 2004
  2. theotyflos
    Replies:
    3
    Views:
    444
    Thomas Matthews
    Feb 19, 2004
  3. robin liu
    Replies:
    3
    Views:
    806
    Robin Liu
    Apr 21, 2006
  4. Replies:
    9
    Views:
    916
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    437
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page