Ajay,

Mukul's solution should work regardless of what the name of the root element is. That is why <xsl:copy-of select="*/Request/RequestHeader"/> was used. Note that the '*' means it does not care about the name of the root element, it will simply copy the Request/RequestHeader child nodes, which seems to be the problem you want to solve.

You do not really need to create a variable with the name of the root element, and then make a decision based on that name (from what we understand of your problem).

However if you find that you need to use the value of a string variable as part of an XPath expression (which might be what you were originally thinking of doing), you can use Xalan's evaluate() extension function, or EXSLT's implementation of the same (EXSLT is a standard set of extension functions). This evaluates a string as an XPath expression.

For example (using Xalan's own evaluate() function):

<xsl:copy-of select="xalan:evaluate(concat($v1,'/Request/RequestHeader'))"/>

This would build a string using the string value of $v1 (assuming here that $v1 contains the name of your root element), and then it would evaluate the resulting string as an XPath expression. This would end up copying the desired nodes to the output tree.

In order to use Xalan's extension functions, you must include the "xalan" namespace in your XSLT file:
xmlns:xalan="http://xml.apache.org/xslt";

Note that XSLT itself does not have a built in evaluation function, which is why vendors provide their own extension functions for this purpose.

See http://xml.apache.org/xalan-j/extensionslib.html for information on using extension functions in Xalan.

Good luck,
Nathan

ajay bhadauria wrote:
Hi Mukul,

Thanks a lot for reply.

The solution you gave will work. But in my xml file, the root element of document could SendRequest or ExchangeRequest. So I was thinking that I could define a variable and based upon the value of this variable ( SendRequest or ExchangeRequest) I can copy all the elements.

Sorry about that I have not mentioned in my last query.

So , if there anything is anything in the XSL which I can use it and convert text to node.


Regards
Ajay

--- On *Sat, 11/19/11, Mukul Gandhi /<gandhi.mu...@gmail.com>/* wrote:


    From: Mukul Gandhi <gandhi.mu...@gmail.com>
    Subject: Re: How to convert string to Node in xslt
    To: "ajay bhadauria" <abhadau...@yahoo.com>
    Cc: xalan-j-users@xml.apache.org
    Date: Saturday, November 19, 2011, 3:15 PM

    Hi Ajay,
        From your problem description, it seems you effectively need
    an XSLT stylesheet like following,

    <xsl:stylesheet version="1.0" xmlns:SwInt="some-namespace-ref">

         <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
             <SwInt:RequestDescriptor>
                <xsl:copy-of select="*/Request/RequestHeader"/>
            </SwInt:RequestDescriptor>
         </xsl:template>

    </xsl:stylesheet>

    (not tested)

    and it seems, you wouldn't need a text to node facility.

    Please feel free to report, if above solution doesn't work, and we
    could possible suggest something different.

    On Sat, Nov 19, 2011 at 3:27 AM, ajay bhadauria
    <abhadau...@yahoo.com </mc/compose?to=abhadau...@yahoo.com>> wrote:

        Hi ,
I have a variable in my xslt and based on the value of the
        variable, I need to select the xpath but the variable is not
        getting translated to node since it is text. So how I can
        convert text(string ) to node. Is there any alternate way of
        doing it ?
Regards
        Ajay
My XSLT is <xsl:stylesheet version="1.0" >
         <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
         <xsl:template match="/">
          <xsl:variable name="v1">
           <xsl:for-each select="/SendRequest|/ExchangeRequest">
            <xsl:choose>
             <xsl:when test="(local-name()='SendRequest')">
              <xsl:value-of select="SendRequest"/>
             </xsl:when>
              <xsl:when test="(local-name()='ExchangeRequest')">
              <xsl:value-of select="ExchangeRequest"/>
             </xsl:when>
            </xsl:choose>
           </xsl:for-each>
          </xsl:variable>
          <SwInt:RequestDescriptor>
           <xsl:copy-of select="$v1/Request/RequestHeader"/>
          </SwInt:RequestDescriptor>
         </xsl:template>
        </xsl:stylesheet>
--------------------------
        XML is
        <SendRequest >
         <Request>
          <RequestHeader>
           <Requestor>xyz</Requestor>
           <Responder>abc</Responder>
           <RequestType>test</RequestType>
</RequestHeader> </Request>
        </SendRequest>






-- Regards,
    Mukul Gandhi



Reply via email to