XSL Transform Control Display Name

Discussion in 'XML' started by Ravi, Nov 13, 2003.

  1. Ravi

    Ravi Guest

    My XML looks like:

    <abc>
    <def type="apple"> 1 </def>
    <def type="peach"> 2 </def>
    <def type="orange"> 3 </def>
    <def type="banana"> 4 </def>
    <def type="plum"> 5 </def>
    </abc>

    I can write the transform to have this display as

    apple 1
    peach 2
    orange 3
    banana 4
    plum 5

    However I want to create more flexibility in displaying the names. So
    say I have an external text file something like:

    apple Xapple
    peach Xpeach
    orange Xorange
    banana Xbanana
    plum Xplum

    the first column is the internal names i.e. actual names as found in the
    XML and the second column is user-defined (external names and hence
    variant). So I want the XML to be displayed as

    Xapple 1
    Xpeach 2
    Xorange 3
    Xbanana 4
    Xplum 5

    i.e. I want to get the external name corresponding to the internal name
    from the file and use that external name to display the value of the
    element. This way a user can control the names used in the display by
    merely editing the external text file and not having to work with
    XML/XSLT. Any suggestions/pointers will be highly appreciated.

    TIA.
     
    Ravi, Nov 13, 2003
    #1
    1. Advertising

  2. Ravi

    Ravi Guest

    Ravi wrote:

    > My XML looks like:
    >
    > <abc>
    > <def type="apple"> 1 </def>
    > <def type="peach"> 2 </def>
    > <def type="orange"> 3 </def>
    > <def type="banana"> 4 </def>
    > <def type="plum"> 5 </def>
    > </abc>
    >
    > I can write the transform to have this display as
    >
    > apple 1
    > peach 2
    > orange 3
    > banana 4
    > plum 5
    >
    > However I want to create more flexibility in displaying the names. So
    > say I have an external text file something like:
    >
    > apple Xapple
    > peach Xpeach
    > orange Xorange
    > banana Xbanana
    > plum Xplum
    >
    > the first column is the internal names i.e. actual names as found in the
    > XML and the second column is user-defined (external names and hence
    > variant). So I want the XML to be displayed as
    >
    > Xapple 1
    > Xpeach 2
    > Xorange 3
    > Xbanana 4
    > Xplum 5
    >
    > i.e. I want to get the external name corresponding to the internal name
    > from the file and use that external name to display the value of the
    > element. This way a user can control the names used in the display by
    > merely editing the external text file and not having to work with
    > XML/XSLT. Any suggestions/pointers will be highly appreciated.


    I thought of using Javascript (cannot read an external file so I
    hardcoded the internal-external mappings into an associative array as
    shown below.

    <xsl:script language="javascript" implements-prefix="user">
    <![CDATA[
    function GetExternal(internal)
    {
    var my_cars= new Array()
    my_cars["apple"]="Xapple";
    my_cars["peach"]="Xpeach";
    my_cars["orange"]="Xorange";
    my_cars["banana"]="Xbanana";
    my_cars["plum"]="Xplum";
    return my_cars[internal];
    }
    ]]>
    </xsl:script>



    and when I want to do the display I do

    <xsl:template match="def">
    <xsl:variable name="tmp"><xsl:value-of select="@type"/> </xsl:variable>
    <xsl:value-of select="user:GetExternal($tmp)" />
    <xsl:value-of select="."/>
    </xsl:template>


    but it does not work saying xsl:script cannot be declared in stylesheet
    (or namespace?). Can someone kindly shed some light on how to use
    javascript in xsl? Or can this kind of mapping be done at the XSL level
    itself? Your comments are highly appreciated.

    Thanks.
     
    Ravi, Nov 13, 2003
    #2
    1. Advertising

  3. In article <bp0n4p$eg0$>,
    Ravi <> wrote:

    % However I want to create more flexibility in displaying the names. So
    % say I have an external text file something like:
    %
    % apple Xapple
    % peach Xpeach
    % orange Xorange
    % banana Xbanana
    % plum Xplum

    You can encode the external file in xml and access it using document()
    in an XPath expression.

    fruit.xml:

    <fruit>
    <name type='apple'>Xapple</name>
    <name type='peach'>Xpeach</name>
    <name type='orange'>Xorange</name>
    <name type='banana'>Xbanana</name>
    <name type='plum'>Xplum</name>
    </fruit>

    Then instead of
    <xsl:template match='def'>
    <xsl:value-of select='@type'/>
    ...
    <xsl:template match='def'>

    you could write

    <xsl:template match='def'>
    <xsl:variable name='type' select='@type'/>
    <xsl:value-of select='document("fruit.xml")/fruit/name[@type=$type]'/>
    ...
    <xsl:template match='def'>

    You could have the list of names as part of your stylesheet if that's
    more convenient

    <mf:fruit>
    <mf:name type='apple'>Xapple</mf:name>
    <mf:name type='peach'>Xpeach</mf:name>
    <mf:name type='orange'>Xorange</mf:name>
    <mf:name type='banana'>Xbanana</mf:name>
    <mf:name type='plum'>Xplum</mf:name>
    </mf:fruit>

    <xsl:template match='def'>
    <xsl:variable name='type' select='@type'/>
    <xsl:value-of select='document("")/mf:fruit/mf:name[@type=$type]'/>
    ...
    <xsl:template match='def'>

    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Nov 14, 2003
    #3
    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. KathyB
    Replies:
    0
    Views:
    1,036
    KathyB
    Jun 25, 2003
  2. Cowboy \(Gregory A. Beamer\)

    Re: How do i transform an xml with 2 xsl files?

    Cowboy \(Gregory A. Beamer\), Jul 3, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    304
    Showjumper
    Jul 3, 2003
  3. Showjumper
    Replies:
    1
    Views:
    309
    Showjumper
    Jul 3, 2003
  4. suzy
    Replies:
    0
    Views:
    562
  5. Replies:
    1
    Views:
    3,637
    A. Bolmarcich
    May 27, 2005
Loading...

Share This Page