You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by ajay bhadauria <ab...@yahoo.com> on 2011/11/18 22:57:28 UTC

How to convert string to Node in xslt

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>

Re: How to convert string to Node in xslt

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Ajay,
   I'm afraid I don't have any more solutions just now, for this problem.
Something not working at your end, may require solutions specific to your
technical environment which we might not be aware of.

I would also suggest to move this and future discussions about generic XSLT
questions on the XSL-List (subscribing info at,
http://www.mulberrytech.com/xsl/xsl-list/index.html#subscribing). If
there's anything which is Xalan specific or an XSLT 1.0 requirement which
must be solved with Xalan, I think this list would still be the right place
to discuss those topics.

On Tue, Nov 22, 2011 at 7:54 PM, ajay bhadauria <ab...@yahoo.com>wrote:

> Mukul,
>
> Thanks a lot !
>
> But it did not work.
>
> <xsl:element name="{$v1}">
>      <xsl:copy-of select="*/Request/RequestHeader"/>
> </xsl:element>
> ----> it says that it requires QNAME. I tried to use "($v1)" , It was
> simply ignoring.
>
> 2)
>
> xsl:copy-of select="*[local-name() = $v1]/Request/RequestHeader"/>
>
> This was also ignoring siliently.
>
> I found something nodeset on Apache website which converts string to
> nodeset but due
> some reason that also does not work.
>
>   xmlns:xalan="http://xml.apache.org/xalan"
>                    exclude-result-prefixes="xalan">
> <xsl:copy-of select="xalan:nodeset($v1)/Request/RequestHeader"/>
>
> I do not know why this does not. It is also simply ignoring. I am using
> xalan-2.7.1
>
> Regards
> Ajay
>





-- 
Regards,
Mukul Gandhi

Re: How to convert string to Node in xslt

Posted by ajay bhadauria <ab...@yahoo.com>.
Mukul,
 
Thanks a lot !
 
But it did not work.
 
<xsl:element name="{$v1}">
     <xsl:copy-of select="*/Request/RequestHeader"/>
</xsl:element>

----> it says that it requires QNAME. I tried to use "($v1)" , It was simply ignoring.
 
2)
 
xsl:copy-of select="*[local-name() = $v1]/Request/RequestHeader"/>

This was also ignoring siliently.  
 
I found something nodeset on Apache website which converts string to nodeset but due 
some reason that also does not work.
 
  xmlns:xalan="http://xml.apache.org/xalan"
                   exclude-result-prefixes="xalan">

<xsl:copy-of select="xalan:nodeset($v1)/Request/RequestHeader"/>
 
I do not know why this does not. It is also simply ignoring. I am using xalan-2.7.1
 
Regards
Ajay

--- On Sun, 11/20/11, Mukul Gandhi <ga...@gmail.com> wrote:


From: Mukul Gandhi <ga...@gmail.com>
Subject: Re: How to convert string to Node in xslt
To: "ajay bhadauria" <ab...@yahoo.com>
Cc: xalan-j-users@xml.apache.org
Date: Sunday, November 20, 2011, 10:33 AM


Hi Ajay,
    Let's assume your variable "v1" would finally have a value in it (based on some selection logic that you want to write, which essentially extracts data from your input XML document), as below,

<xsl:variable name="v1" select="'SendRequest'"/>   <!-- or can have value 'ExchangeRequest' -->

(I've directly written the value for this variable, but in your case the value would be computed as you wrote)

You now essentially want to copy a node to output XSLT tree as below,

<xsl:copy-of select="$v1/Request/RequestHeader"/>

Of course this would not work, since 'v1' here is not a node and is a string value.

But I think, the following would probably work,

<xsl:copy-of select="*[local-name() = $v1]/Request/RequestHeader"/>

Or perhaps something like below would also work (which is closer to what you're asking i.e text to a node),

<xsl:element name="{$v1}">
     <xsl:copy-of select="*/Request/RequestHeader"/>
</xsl:element>

Does any of above approaches suites you better?


On Sat, Nov 19, 2011 at 10:12 PM, ajay bhadauria <ab...@yahoo.com> 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 






-- 
Regards,
Mukul Gandhi

Re: How to convert string to Node in xslt

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Ajay,
    Let's assume your variable "v1" would finally have a value in it (based
on some selection logic that you want to write, which essentially extracts
data from your input XML document), as below,

<xsl:variable name="v1" select="'SendRequest'"/>   <!-- or can have value
'ExchangeRequest' -->

(I've directly written the value for this variable, but in your case the
value would be computed as you wrote)

You now essentially want to copy a node to output XSLT tree as below,

<xsl:copy-of select="$v1/Request/RequestHeader"/>

Of course this would not work, since 'v1' here is not a node and is a
string value.

But I think, the following would probably work,

<xsl:copy-of select="*[local-name() = $v1]/Request/RequestHeader"/>

Or perhaps something like below would also work (which is closer to what
you're asking i.e text to a node),

<xsl:element name="{$v1}">
     <xsl:copy-of select="*/Request/RequestHeader"/>
</xsl:element>

Does any of above approaches suites you better?

On Sat, Nov 19, 2011 at 10:12 PM, ajay bhadauria <ab...@yahoo.com>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
>





-- 
Regards,
Mukul Gandhi

Re: How to convert string to Node in xslt

Posted by ajay bhadauria <ab...@yahoo.com>.
Nathan

Thanks a lot for reply. The solution you gave works. The solution which Mukul gave also  works when I use */Request irrespective of root element.

Thanks a lot for all help ! problem solved

Regards
Ajay 

--- On Tue, 11/22/11, Nathan Nadeau <nd...@gleim.com> wrote:

From: Nathan Nadeau <nd...@gleim.com>
Subject: Re: How to convert string to Node in xslt
To: "ajay bhadauria" <ab...@yahoo.com>
Cc: xalan-j-users@xml.apache.org
Date: Tuesday, November 22, 2011, 11:21 PM




  
  

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 <ga...@gmail.com>
wrote:

        

From: Mukul Gandhi <ga...@gmail.com>

Subject: Re: How to convert string to Node in xslt

To: "ajay bhadauria" <ab...@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 <ab...@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

          
        
        
      
    
  







Re: How to convert string to Node in xslt

Posted by Nathan Nadeau <nd...@gleim.com>.
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 /<ga...@gmail.com>/* wrote:
>
>
>     From: Mukul Gandhi <ga...@gmail.com>
>     Subject: Re: How to convert string to Node in xslt
>     To: "ajay bhadauria" <ab...@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
>     <abhadauria@yahoo.com </m...@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
>



Re: How to convert string to Node in xslt

Posted by ajay bhadauria <ab...@yahoo.com>.
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 <ga...@gmail.com> wrote:

From: Mukul Gandhi <ga...@gmail.com>
Subject: Re: How to convert string to Node in xslt
To: "ajay bhadauria" <ab...@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 <ab...@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


Re: How to convert string to Node in xslt

Posted by Mukul Gandhi <ga...@gmail.com>.
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 <ab...@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