Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSL Transform Control Display Name

Reply
Thread Tools

XSL Transform Control Display Name

 
 
Ravi
Guest
Posts: n/a
 
      11-13-2003
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.














 
Reply With Quote
 
 
 
 
Ravi
Guest
Posts: n/a
 
      11-13-2003
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.


 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      11-14-2003
In article <bp0n4p$eg0$(E-Mail Removed)>,
Ravi <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
Is it possible to do a xsl transform on a string? Showjumper ASP .Net 1 07-03-2003 08:54 PM
Re: How do i transform an xml with 2 xsl files? Cowboy \(Gregory A. Beamer\) ASP .Net 1 07-03-2003 02:35 PM
What parses the xml/xsl transform in asp.net? System.xml NOT msxml? KathyB ASP .Net 0 06-25-2003 05:03 PM



Advertisments