Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Manipulation (substring) Xpath Question

Status
Not open for further replies.

inarobis

IS-IT--Management
Apr 3, 2006
71
0
0
CH
Hello guys,

I have question about manipulation string with XPath

I would like to know how can I transform something like this:

North America in

NOR_AME

So The first three letter of the first word and others 3 letter of the second word.


I have liste of regions:

Asia ASI
Pacific PAC
South America SOU_AME

I would like to have always the first 3 letters of the word.

Any suggestion how to do that.

Ina

 
Ina,

There is no pure XPath mechanism, I don't think. But you certainly can do this within XSLT.

Create a XSLT stylesheet with a lookup table as a top-level element. You must use a namespace to distinguish this element from other XSL top-level elements.
Code:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
 xmlns:r="[URL unfurl="true"]http://regions.data">[/URL]
 
<!-- To use the lookup table you would use the following 
     (in appropriate context, of course): 
-->
	<xsl:value-of select="document('')/*/r:regions/r:region[@name = .]/@abbr"/>

<!-- You may place the lookup table at the end of the stylesheet 
	(it is still a top-level element 
-->
 <r:regions>
  <r:region abbr="NOR_AME" name="North America"/>
  <r:region abbr="ASI" name="Asia"/>
  <r:region abbr="PAC" name="Pacific"/>
  <r:region abbr="SOU_AME" name="South America"/>
 </r:regions>
</xsl:stylesheet>
(Note: untested, could have some typing errors)

This is a very typical lookup mechanism. The select uses the document('') function -- with an empty string as an argument -- to refer to the stylesheet itself. It then uses standard XPath syntax to look up the abbreviation that corresponds to the region name. You can use a similar mechanism to have an external XML document function as a lookup table.

You could also use a recursive template if you have an arbitrary region name, but the lookup is probably better if you have a specific list of regions.

Tom Morrison
 
Thank you! :)

I will try that. I am not very strong on xslt extension as here xmlns:r and did not understant the
Ina
 
Ina,

That which you do not understand is the concept of namespace. You may read a bit about the namespace concept here.

[tt][ignore] [/ignore][/tt] is a URI, Universal Resource Indicator. In this case it is a unique string (within the context of your application) which is used to differentiate the namespace named 'r'. It is not uncommon to confuse a URI with a URL; a URL is a URI, but not all URIs are URLs. I used this particular string for the URI because it helps document what is being defined in the elements that use that namespace.

Now all this is sort of mysterious, but in this case one must use a 'foreign' namespace (that is, a URI not standardized for use by XSLT) on a top-level element (an element immediately subordinate to <xsl:stylesheet> to implement the table-lookup mechanism. This requirement is in the XSLT specification (hidden as a requirement that the XSLT processor must ignore unknown top-level elements without error). Since the 'known' (i.e. standardized) XSLT top-level elements provide processing instructions, this is the way to introduce tabular data constants into your stylesheet.

Tom Morrison
 
Thank you again!

Now things are much more clearer in my head now. Thank you for your help.

Ina

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top