Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another one

 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
23)
What is the result of the following XPath expression?
<xsl:value-of select="translate('--aaa--','aba-','ABC')" />

a) --aaa--

b) --AAA--

c) AAA

d) AaC

This one is also not clear to me. A will replace a, how about B and C? Since "-" does not appear in "ABC", it should be deleted from the first part, right? b) or c)?
 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, B is not relevant since no b in the first part. But for C, is there any overriding rule for this one?
 
Svetlana Petrova
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's c). And it's an exact example from XPath Recomendation (http://www.w3.org/TR/xpath , search for "translate").
 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Svetlana Petrova:
It's c). And it's an exact example from XPath Recomendation (http://www.w3.org/TR/xpath , search for "translate").

Thanks, Svetlana. I have not read these recommendations yet. Seems I cannot be lazy.
 
Vasan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
when we use
<xsl:value-of select="translate('--aaa--','aba--','ABC')" />
returns AAA
whereas when we use
<xsl:value-of select="translate('--aaa--','-aba--','ABC')" />
returns AABBBAA
can any one pls explain how this is coming?
Vasan
 
Siva shanmugam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the rule while using translate() function
translate(OrginalString,SecondArg,ThirdArg)
1. If a character in the original string appears in the second argument string,its is replaced with the "corresponding " character in the third argument string
2.If a character in the originalstring appears in the second argument string and there is no corresponding character in the third argument string,then the character is deleted.
3.If a character in the second argument string appear more than once,the first occurance determines the replacement character.
4.If the third argument string is larger than the second string extra characters are ignored.
Ref:- XSLT by Doug Tidwell
 
Samba Siva Rao Potla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
Take care of this concept, I got this type of question in Exam.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xalan does return AAA for -
<xsl:value-of select="translate('--aaa--','aba--','ABC')" />
XML Spy 4.4 on the other hand, didn't read the specification.
It returns AAaaaAA.
BTW, XML Spy 5 is out. According to Altova -
XMLSPY 5 - Builds on the previous XMLSPY version by adding XSLT debugging, WSDL editing, Java/C++ code generation, HTML Importing, Tamino Integration, and much more.

Cheers,
Dan
 
Svetlana Petrova
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example:
<xsl:value-of select="translate('--aaa--','-aba--','ABC')" />
returns AABBBAA
Parser does the following:
- takes 1st character from the 1st argument, it's an '-';
- checks 2d argument, finds 1st '-' in 1st position;
- checks 1st position of the 3d argument, finds 'A';
- replaces '-' in 1st argument with 'A';
- repeats the same for the 2d '-' in the 1st argument;
- in 3d position of the 1st argument it finds
'a';
- checks 2d arument and finds 'a' there in the
2d position;
- checks 2d postion of the 3d argument and finds 'B';
- replaces 'a' in the 1st argument with 'B';
- repeats the same with other 2 'a's in the 1st argument;
- repeats the steps above for another 2 '-'s in the 1st argument.
Fun, ah?!!
 
ZEESHAN AZIZ
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ref: Begining XML : p:156
translate ('+bat+','abc+', 'ABC') will result in "BAt". I am not clear as to how can we get t IN result string, when there in no t at all in third argument.
Can anyone clarify whether is it a typo, or I am mistaken.
 
satya uppaluri
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
translate ('+bat+','abc+', 'ABC')
I think it works like this.
First forget about first argument, which is to be translated. Start building the mapping.
Take 1st character �a� in 2nd argument -- map it to 1st character �A� in 3rd argument.
Take 2nd character �b� in 2nd argument -- map it to 2nd character �B� in 3rd argument.
Take 3rd character �c� in 2nd argument -- map it to 3rd character �C� in 3rd argument.
Take 4th character �+� in 2nd argument -- map it to 4th character in 3rd argument i.e. blank.
Now start translation of original string �+bat+�.
For �+� mapping is blank string.
For �b� mapping is there so replace it with �B�
For �a� mapping is there so replace it with �A�
For �t� no mapping so leave it as �t�.
So finally the result is �Bat�
Am I correct guys? If not please correct me. This is very important and tricky one and there is every chance to appear in exam.
 
ZEESHAN AZIZ
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi satya uppaluri,
Your explanation is perfect. Here is what I learned from all this discussion.
1. Translate() function goes through the first argument, character by character.
2. For each character of first argument, it searches for a match in the second argument, and remember its index.
3. This index is then used to find a character in the third argument, which is used to replace the characters in the first string.
4. When a character exists in the first argument that does not exist in the second argument, then the character is not replaced, but stays as it is, as is evident from the last problem I mentioned.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic