aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSLT  value-of parameter not works as xml value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT  value-of parameter not works as xml value" Watch "XSLT  value-of parameter not works as xml value" New topic
Author

XSLT value-of parameter not works as xml value

Sundar Pari
Greenhorn

Joined: May 04, 2011
Posts: 11
Hello ,

I am new to xslt.

I have set of parameters which is passed from a java program like

<xsl : param name="myParam"></xsl : param>

I wrote a simple String replace function which would replace strings based on key-value pair method

<string_replacement>
<search>
<find>{$placeHolder$}</find>
<replace><xsl:value-of select="$myParam"/></replace>
</search>
</string_replacement>

I will use the above to replace my placeholder by the value passed by java program at runtime. I call my template like this :
<xsl:call-template name="string-replacell">
<xsl:with-param name="text" select="$input_text" />
<xsl:with-param name="replace" select="$search[1]/find" />
<xsl:with-param name="newValue" select="$search[1]/replace"/>
</xsl:call-template>

I am unable to proceed further because, I am getting empty string replaced. if I put some hardcoded value in replace like <replace>hardCodedValue</replace> then it works fine.

I hope there is a way to make it work.

Thanks in advance

-Sundar
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 507
    
    3
It is not clear what is in where... Howabout outline the "original xml document", the "xsl document" and put the blocks of elements so posted within them at their proper places. Also show the named template with their param tags as well...
Sundar Pari
Greenhorn

Joined: May 04, 2011
Posts: 11
Thanks for your reply.

Here is my xml document

<Templates>

<Template id="template1">
<head>this is my head</head>
<body> I am {$myName$} </body>
<tail> {$someMessage$}</tail>
</Template>

<Template id="template2">
<head>this is my secondtemplate</head>
<body> I am {$working$} </body>
<tail> {$someMessage$}</tail>
</Template>

<Templates>

All I need is to write a xsl which takes templateId and parameters as input from a java code and it should choose the correct template from xml file and replace all the placeholders in the provided templateId.

Here is my xsl

<!-- Parameter Declaration -->
<xsl:param name="templateId"></xsl:param>
<xsl:param name="myName"></xsl:param>
<xsl:param name="working"></xsl:param>

<string_replacement>
<search>
<find>{$working$}</find>
<replace>$working</replace> <!-- $working should be value of working parameter -->
</search>
</string_replacement>

<!-- Method for String replace -->
<xsl:template name="replace_strings">
<xsl:param name="input_text" />
<xsl:param name="search" select="document('')/*/string_replacement/search" />
<xsl:variable name="replaced_text">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="$input_text" />
<xsl:with-param name="replace" select="$search[1]/find" />
<xsl:with-param name="by" select="$search[1]/replace"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$search[2]">
<xsl:call-template name="replace_strings">
<xsl:with-param name="input_text" select="$replaced_text" />
<xsl:with-param name="search" select="$search[position() > 1]" />
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:value-of select="$replaced_text" />
</xsltherwise>
</xsl:choose>
</xsl:template>

<!-- Method for Sub-String replace -->
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text"
select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:value-of select="$text" />
</xsltherwise>
</xsl:choose>
</xsl:template>


g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 507
    
    3
[0] With your further elucidation, I don't see in what sense
>if I put some hardcoded value in replace like <replace>hardCodedValue</replace> then it works fine.
Hence, either you have something to hide intentionally, or I can only discharge myself from helping. The whole thing contains no working template to put the xslt engine to work at all. Hence, how come it can work or do anything at all?

[1] Besides, the block <string_replacement>...</string_replacement> being in null namespace is impossible. There is no serious xslt processor to my knowledge would accept that kind of top-level element of no namespace in the xslt document. It must be in some non-null namespace to begin with. And then, this is in direct contradition to your saying "it works fine" if some hardcoded value is given to the replace element's content.
Sundar Pari
Greenhorn

Joined: May 04, 2011
Posts: 11
[0] I have not hided anything intentionally.. I thought those lines are enough to debug, this may be because of inexperience in xslt.. I am attaching the xsl and xml files, which is slightly working for me for your kind reference.

Thanks a lot for your help!

Since it is not allowing me to attach files . I am pasting the contents here..


g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 507
    
    3
Thanks for the effort. That does disperse crucial obscurity. A couple of comments are in order.

[2] As mentioned in [1], the way a "look-up" table constructed inside the xslt document cannot belong to the null namespace is still applicable. That defect is curable by simply adding some namespace specifically to the <string_replacement> block. I won't be doing this in this case of trying to "dynamically" setting up the table's value. It won't work that way. <xsl:value-of> or attribute value template won't help as they must be in some template called upon to make the evaluation. (If anything happens that make you think it work by hard coding the param or whatever..., it is just some kind of illusion.) There are some questionable constructions in the rest which may contribute to that illusion. But I am not going to specifically picking them one-by-one or to scrutinize them, that demands a lot of efforts and verbosity! (If one is determined to make that kind of construction work, one has to use some extension. I am quite sure it can be made with some effort, but I don't think it is particularly attractive to doing so.)

[3] As a quick and direct approach, you should be able to do it like this within the realm of version 1.0 without using any xslt extension functionality.

[3.1] I would add mode attribute to isolate the specific processing need for that objective.
[3.2] With the above, the replacement of value for each placeholder is thorough, not restricted to a specific subset of them for a specific Template id...
[3.3] The placeholder can appear at multiple places in the text content.

[4] When think about it, the scripting of template to match the text() of mode="proc" is not less generally applicale as drafting a lookup table. It might look more laborious by cascading the replacement.
Sundar Pari
Greenhorn

Joined: May 04, 2011
Posts: 11
Hello tsuji,

You have solved my problem in style.

Thanks a zillion. This is a timely help I have ever recieved..

Thanks a lottttt

I really appreciate the way you looked into the problem and your way of explaining things is PERFECT.

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 507
    
    3
Thanks, Sundar Pari, for the feedback. Am glad you find it helpful as the step forward.

I take this opportunity to rectify one overlook. The template element:
><xsl:template match="*|@*|text()" mode="proc">
should be read:

without the matching of text(). The proper text() template is the one by good chance after it which would win out in priority rule. Should it be behind the proper template, it would have won against the purpose.

ps: I decide to edit the post first profiting the edit button is still available at this moment. This post serves as documenting this amendment.
Sundar Pari
Greenhorn

Joined: May 04, 2011
Posts: 11
Thanks for the optimization. Am marking this thread as solved...
 
jQuery in Action, 2nd edition
 
subject: XSLT value-of parameter not works as xml value