By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

XSL Transform Control Display Name

P: n/a
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.




Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
Jul 20 '05 #2

P: n/a
In article <bp**********@prometheus.acsu.buffalo.edu>,
Ravi <rg**@cse.buffalo.edu> 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
pt**@interlog.com
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.