XSLT indirect variable lookup

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zachary  Turner

    #1

    XSLT indirect variable lookup

    Let's say I have a variable defined as follows:

    <xsl:variable name="test_vari able_1" value="'test_va lue_A'"/>
    <xsl:variable name="test_vari able_2" value="'test_va lue_B'"/>
    <xsl:variable name="test_vari able_3" value="'test_va lue_C'"/>

    Then, somewhere else in my source document I have some elements like
    this:

    <some-doc-element param="2"/>
    <some-doc-element param="3"/>
    <some-doc-element param="1"/>

    I want to transform this into the following:

    <destination-element value="'test_va lue_B'"/>
    <destination-element value="'test_va lue_C'"/>
    <destination-element value="'test_va lue_A'"/>

    Essentially, this would consist of a couple steps:

    1) Reading the value of the proper source element attribute
    2) Programmaticall y create a string that specifies the name of the
    variable to look up
    3) Retrieve the 'value' parameter of the variable determined in #2.
    4) Output the value determined in #3 to the document.

    Step 3 is what I don't know how to do. I have the -name- of a
    variable stored in the value of another variable, and I want to use
    that variable to find the variable with that name.

    Is something like this even possible? I can make a huge if statement
    if necessary, but this seems more elegant, and easier to understand.

    Thanks

  • Bjoern Hoehrmann

    #2
    Re: XSLT indirect variable lookup

    * Zachary Turner wrote in comp.text.xml:
    >Let's say I have a variable defined as follows:
    >
    ><xsl:variabl e name="test_vari able_1" value="'test_va lue_A'"/>
    ><xsl:variabl e name="test_vari able_2" value="'test_va lue_B'"/>
    ><xsl:variabl e name="test_vari able_3" value="'test_va lue_C'"/>
    This is not a good idea. I would recommend to use something like

    <my:map xmlns:my='http://example.org/...'>
    <my:item key='1' value='test_val ue_A' />
    <my:item key='2' value='test_val ue_B' />
    ...
    >Then, somewhere else in my source document I have some elements like
    >this:
    >
    ><some-doc-element param="2"/>
    ><some-doc-element param="3"/>
    ><some-doc-element param="1"/>
    >
    >I want to transform this into the following:
    >
    ><destination-element value="'test_va lue_B'"/>
    ><destination-element value="'test_va lue_C'"/>
    ><destination-element value="'test_va lue_A'"/>
    .... then you can simply use something like

    ...
    <xsl:variable name='param' select='@param' />
    <destination-element value="{
    document('')//my:map/my:item[ @key = $param ]/@value
    }"/>
    ...

    The document('') refers to the XSLT document, then it looks up the map
    in it based on the key (the param attribute) and uses the value of the
    value attribute in the map in the output.
    >3) Retrieve the 'value' parameter of the variable determined in #2.
    That is not possible using only XSLT 1.0 features, and poor design.
    --
    Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

    Comment

    • Martin Honnen

      #3
      Re: XSLT indirect variable lookup

      Zachary Turner wrote:
      Let's say I have a variable defined as follows:
      >
      <xsl:variable name="test_vari able_1" value="'test_va lue_A'"/>
      <xsl:variable name="test_vari able_2" value="'test_va lue_B'"/>
      <xsl:variable name="test_vari able_3" value="'test_va lue_C'"/>
      >
      Then, somewhere else in my source document I have some elements like
      this:
      >
      <some-doc-element param="2"/>
      <some-doc-element param="3"/>
      <some-doc-element param="1"/>
      >
      I want to transform this into the following:
      >
      <destination-element value="'test_va lue_B'"/>
      <destination-element value="'test_va lue_C'"/>
      <destination-element value="'test_va lue_A'"/>
      <xsl:template match="some-doc-element[@param = '1'">
      <destination-element value="{$test_v ariable_1}"/>
      </xsl:template>

      <xsl:template match="some-doc-element[@param = '2'">
      <destination-element value="{$test_v ariable_2}"/>
      </xsl:template>

      <xsl:template match="some-doc-element[@param = '3'">
      <destination-element value="{$test_v ariable_3}"/>
      </xsl:template>


      That will output e.g.
      <destination-element value="test_val ue_B"/>
      If you really want
      <destination-element value="'test_va lue_B'"/>
      then you need

      <xsl:template match="some-doc-element[@param = '1'">
      <destination-element value="'{$test_ variable_1}'"/>
      </xsl:template>

      <xsl:template match="some-doc-element[@param = '2'">
      <destination-element value="'{$test_ variable_2}'"/>
      </xsl:template>

      <xsl:template match="some-doc-element[@param = '3'">
      <destination-element value="'{$test_ variable_3}'"/>
      </xsl:template>



      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • Ixa

        #4
        Re: XSLT indirect variable lookup

        3) Retrieve the 'value' parameter of the variable determined in #2.
        [...]
        Is something like this even possible?
        Yes, but only by an extension.




        --
        Ixa

        Comment

        • Dimitre Novatchev

          #5
          Re: XSLT indirect variable lookup

          Yes, it is possible and it is not a good design.

          Cheers,
          Dimitre Novatchev.


          "Zachary Turner" <divisortheory@ gmail.comwrote in message
          news:1180533781 .522342.302460@ u30g2000hsc.goo glegroups.com.. .
          Let's say I have a variable defined as follows:
          >
          <xsl:variable name="test_vari able_1" value="'test_va lue_A'"/>
          <xsl:variable name="test_vari able_2" value="'test_va lue_B'"/>
          <xsl:variable name="test_vari able_3" value="'test_va lue_C'"/>
          >
          Then, somewhere else in my source document I have some elements like
          this:
          >
          <some-doc-element param="2"/>
          <some-doc-element param="3"/>
          <some-doc-element param="1"/>
          >
          I want to transform this into the following:
          >
          <destination-element value="'test_va lue_B'"/>
          <destination-element value="'test_va lue_C'"/>
          <destination-element value="'test_va lue_A'"/>
          >
          Essentially, this would consist of a couple steps:
          >
          1) Reading the value of the proper source element attribute
          2) Programmaticall y create a string that specifies the name of the
          variable to look up
          3) Retrieve the 'value' parameter of the variable determined in #2.
          4) Output the value determined in #3 to the document.
          >
          Step 3 is what I don't know how to do. I have the -name- of a
          variable stored in the value of another variable, and I want to use
          that variable to find the variable with that name.
          >
          Is something like this even possible? I can make a huge if statement
          if necessary, but this seems more elegant, and easier to understand.
          >
          Thanks
          >

          Comment

          Working...