IC5Notice: We have upgraded developerWorks Community to the latest version of IBM Connections. For more information, read our upgrade FAQ.
Topic
  • 7 replies
  • Latest Post - ‏2013-02-07T18:54:02Z by Doyler86
Doyler86
Doyler86
91 Posts

Pinned topic Beginner XSLT Help

‏2013-02-07T15:21:28Z |
Hello,
I have looked everywhere and cannot figure this out, so I have come to you guys :).

I have this stylesheet that have the following variables declared:

<xsl:variable name="Domain"
select="/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Domain" />

<xsl:variable name="Class"
select="/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Class" />

<xsl:variable name="Template_Req_Msg">
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<dp:request domain="DOMAIN_PLACEHOLDER"
xmlns:dp="http://www.datapower.com/schemas/management">
<dp:get-status class="CLASS_PLACEHOLDER" />
</dp:request>
</env:Body>
</env:Envelope>
</xsl:variable>
I am trying to build a new variable and replace the Place Holder values with the values from above.

I attempted the value-of statement, but this did not work.

I started playing with regex:replace(), but was unable to make this happen as well.

Any direction or help will be appreciated,

thanks,

Doyle
Updated on 2013-02-07T18:54:02Z at 2013-02-07T18:54:02Z by Doyler86
  • HermannSW
    HermannSW
    4524 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T15:46:52Z  
    <xsl:value-of ...> gives you the string value (that is the concatenation of all text node descendents).

    <xsl:copy-of select="$Template_Req_Msg/*"> should be what you want.

     
    Hermann<myXsltBlog/> <myXsltTweets/>
  • Doyler86
    Doyler86
    91 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T16:31:00Z  
    • HermannSW
    • ‏2013-02-07T15:46:52Z
    <xsl:value-of ...> gives you the string value (that is the concatenation of all text node descendents).

    <xsl:copy-of select="$Template_Req_Msg/*"> should be what you want.

     
    Hermann<myXsltBlog/> <myXsltTweets/>
    Hello Hermann,

    Thank you for your quick response. I am not sure I understand.

    My goal is to create a new variable, using the Template variable as a source, but replacing the Place Holder values with the initial variable declarations.

    Before:
    <xsl:variable name="Template_Req_Msg">
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <dp:request domain="DOMAIN_PLACEHOLDER"
    xmlns:dp="http://www.datapower.com/schemas/management">
    <dp:get-status class="CLASS_PLACEHOLDER" />
    </dp:request>
    </env:Body>
    </env:Envelope>
    </xsl:variable>

    After:

    <xsl:variable name="Template_Req_Msg">
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <dp:request domain="Doyle"
    xmlns:dp="http://www.datapower.com/schemas/management">
    <dp:get-status class="MessageCounter" />
    </dp:request>
    </env:Body>
    </env:Envelope>
    </xsl:variable>

    I tried the following command, but it did not work
    <xsl:variable name="Req_Msg" select="regexp:replace($Template_Req_Msg, 'DOMAIN_PLACEHOLDER', 'g', $Domain)"

    thanks in advance,

    Doyle
  • Doyler86
    Doyler86
    91 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T18:31:53Z  
    • Doyler86
    • ‏2013-02-07T16:31:00Z
    Hello Hermann,

    Thank you for your quick response. I am not sure I understand.

    My goal is to create a new variable, using the Template variable as a source, but replacing the Place Holder values with the initial variable declarations.

    Before:
    <xsl:variable name="Template_Req_Msg">
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <dp:request domain="DOMAIN_PLACEHOLDER"
    xmlns:dp="http://www.datapower.com/schemas/management">
    <dp:get-status class="CLASS_PLACEHOLDER" />
    </dp:request>
    </env:Body>
    </env:Envelope>
    </xsl:variable>

    After:

    <xsl:variable name="Template_Req_Msg">
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <dp:request domain="Doyle"
    xmlns:dp="http://www.datapower.com/schemas/management">
    <dp:get-status class="MessageCounter" />
    </dp:request>
    </env:Body>
    </env:Envelope>
    </xsl:variable>

    I tried the following command, but it did not work
    <xsl:variable name="Req_Msg" select="regexp:replace($Template_Req_Msg, 'DOMAIN_PLACEHOLDER', 'g', $Domain)"

    thanks in advance,

    Doyle
    What I am doing is, I am setting up my request message, so that I can fire off an Internal SOMA call, which will pull back the counter information and return to the client. I am trying to make this dynamic enough that the client can pass in their domain name and class object and Datapower will return the status information for these.

    just giving a little background info on my intent.

    thanks,

    Doyle
  • HermannSW
    HermannSW
    4524 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T18:41:07Z  
    • Doyler86
    • ‏2013-02-07T16:31:00Z
    Hello Hermann,

    Thank you for your quick response. I am not sure I understand.

    My goal is to create a new variable, using the Template variable as a source, but replacing the Place Holder values with the initial variable declarations.

    Before:
    <xsl:variable name="Template_Req_Msg">
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <dp:request domain="DOMAIN_PLACEHOLDER"
    xmlns:dp="http://www.datapower.com/schemas/management">
    <dp:get-status class="CLASS_PLACEHOLDER" />
    </dp:request>
    </env:Body>
    </env:Envelope>
    </xsl:variable>

    After:

    <xsl:variable name="Template_Req_Msg">
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
    <dp:request domain="Doyle"
    xmlns:dp="http://www.datapower.com/schemas/management">
    <dp:get-status class="MessageCounter" />
    </dp:request>
    </env:Body>
    </env:Envelope>
    </xsl:variable>

    I tried the following command, but it did not work
    <xsl:variable name="Req_Msg" select="regexp:replace($Template_Req_Msg, 'DOMAIN_PLACEHOLDER', 'g', $Domain)"

    thanks in advance,

    Doyle
    Hi Doyle,

    sorry that I did not get your problem right the first time.

    You cannot do regexp:replace on XML -- you can do it on strings.
    So this (bad) sequence would work: <dp:serialize ...> regexp:replace() <dp:parse ...>

    Please do it "the XML way" instead.
    You can evaluate an expression in an attribute, see in the spec:
    http://www.w3.org/TR/xslt#attribute-value-templates

    So instead
    domain="DOMAIN_PLACEHOLDER"
    just use
    domain="{$Domain}"

    See the solution in action:
    
    $ coproc2 placeholder.xsl placeholder.xml http:
    //dp2-l3:2223 -s | tidy -iq -xml <env:Envelope xmlns:soapenv=
    "http://schemas.xmlsoap.org/soap/envelope/" xmlns:env=
    "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request domain=
    "domain_1" xmlns:dp=
    "http://www.datapower.com/schemas/management"> <dp:get-status class=
    "class_1" /> </dp:request> </env:Body> </env:Envelope> $
    


    This is the stylesheet:
    
    $ cat placeholder.xsl <xsl:stylesheet version=
    "1.0" xmlns:xsl=
    "http://www.w3.org/1999/XSL/Transform" xmlns:soapenv=
    "http://schemas.xmlsoap.org/soap/envelope/" > <xsl:output omit-xml-declaration=
    "yes" /> <xsl:template match=
    "/"> <xsl:variable name=
    "Domain" select=
    "/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Domain" />   <xsl:variable name=
    "Class" select=
    "/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Class" />     <xsl:variable name=
    "Template_Req_Msg"> <env:Envelope xmlns:env=
    "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request domain=
    "{$Domain}" xmlns:dp=
    "http://www.datapower.com/schemas/management"> <dp:get-status class=
    "{$Class}" /> </dp:request> </env:Body> </env:Envelope> </xsl:variable>   <xsl:copy-of select=
    "$Template_Req_Msg"/>   </xsl:template> </xsl:stylesheet> $
    


    I had to craft a sample input:
    
    $ cat placeholder.xml <?xml version=
    "1.0" encoding=
    "UTF-8"?> <env:Envelope xmlns:env=
    "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <GetMonitorData> <MessageBody> <Domain>domain_1</Domain> <Class>class_1</Class> </MessageBody> </GetMonitorData> </env:Body> </env:Envelope> $
    


     
    Hermann<myXsltBlog/> <myXsltTweets/>
  • Doyler86
    Doyler86
    91 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T18:47:15Z  
    • HermannSW
    • ‏2013-02-07T18:41:07Z
    Hi Doyle,

    sorry that I did not get your problem right the first time.

    You cannot do regexp:replace on XML -- you can do it on strings.
    So this (bad) sequence would work: <dp:serialize ...> regexp:replace() <dp:parse ...>

    Please do it "the XML way" instead.
    You can evaluate an expression in an attribute, see in the spec:
    http://www.w3.org/TR/xslt#attribute-value-templates

    So instead
    domain="DOMAIN_PLACEHOLDER"
    just use
    domain="{$Domain}"

    See the solution in action:
    <pre class="jive-pre"> $ coproc2 placeholder.xsl placeholder.xml http: //dp2-l3:2223 -s | tidy -iq -xml <env:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:env= "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request domain= "domain_1" xmlns:dp= "http://www.datapower.com/schemas/management"> <dp:get-status class= "class_1" /> </dp:request> </env:Body> </env:Envelope> $ </pre>

    This is the stylesheet:
    <pre class="jive-pre"> $ cat placeholder.xsl <xsl:stylesheet version= "1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" > <xsl:output omit-xml-declaration= "yes" /> <xsl:template match= "/"> <xsl:variable name= "Domain" select= "/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Domain" /> <xsl:variable name= "Class" select= "/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Class" /> <xsl:variable name= "Template_Req_Msg"> <env:Envelope xmlns:env= "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request domain= "{$Domain}" xmlns:dp= "http://www.datapower.com/schemas/management"> <dp:get-status class= "{$Class}" /> </dp:request> </env:Body> </env:Envelope> </xsl:variable> <xsl:copy-of select= "$Template_Req_Msg"/> </xsl:template> </xsl:stylesheet> $ </pre>

    I had to craft a sample input:
    <pre class="jive-pre"> $ cat placeholder.xml <?xml version= "1.0" encoding= "UTF-8"?> <env:Envelope xmlns:env= "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <GetMonitorData> <MessageBody> <Domain>domain_1</Domain> <Class>class_1</Class> </MessageBody> </GetMonitorData> </env:Body> </env:Envelope> $ </pre>

     
    Hermann<myXsltBlog/> <myXsltTweets/>
    worked like a charm. :)

    Thanks Hermann,

    I should of figured it would be something small like curly braces,

    thank,

    Doyle
  • HermannSW
    HermannSW
    4524 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T18:48:03Z  
    • HermannSW
    • ‏2013-02-07T18:41:07Z
    Hi Doyle,

    sorry that I did not get your problem right the first time.

    You cannot do regexp:replace on XML -- you can do it on strings.
    So this (bad) sequence would work: <dp:serialize ...> regexp:replace() <dp:parse ...>

    Please do it "the XML way" instead.
    You can evaluate an expression in an attribute, see in the spec:
    http://www.w3.org/TR/xslt#attribute-value-templates

    So instead
    domain="DOMAIN_PLACEHOLDER"
    just use
    domain="{$Domain}"

    See the solution in action:
    <pre class="jive-pre"> $ coproc2 placeholder.xsl placeholder.xml http: //dp2-l3:2223 -s | tidy -iq -xml <env:Envelope xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:env= "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request domain= "domain_1" xmlns:dp= "http://www.datapower.com/schemas/management"> <dp:get-status class= "class_1" /> </dp:request> </env:Body> </env:Envelope> $ </pre>

    This is the stylesheet:
    <pre class="jive-pre"> $ cat placeholder.xsl <xsl:stylesheet version= "1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" xmlns:soapenv= "http://schemas.xmlsoap.org/soap/envelope/" > <xsl:output omit-xml-declaration= "yes" /> <xsl:template match= "/"> <xsl:variable name= "Domain" select= "/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Domain" /> <xsl:variable name= "Class" select= "/soapenv:Envelope/soapenv:Body/GetMonitorData/MessageBody/Class" /> <xsl:variable name= "Template_Req_Msg"> <env:Envelope xmlns:env= "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request domain= "{$Domain}" xmlns:dp= "http://www.datapower.com/schemas/management"> <dp:get-status class= "{$Class}" /> </dp:request> </env:Body> </env:Envelope> </xsl:variable> <xsl:copy-of select= "$Template_Req_Msg"/> </xsl:template> </xsl:stylesheet> $ </pre>

    I had to craft a sample input:
    <pre class="jive-pre"> $ cat placeholder.xml <?xml version= "1.0" encoding= "UTF-8"?> <env:Envelope xmlns:env= "http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <GetMonitorData> <MessageBody> <Domain>domain_1</Domain> <Class>class_1</Class> </MessageBody> </GetMonitorData> </env:Body> </env:Envelope> $ </pre>

     
    Hermann<myXsltBlog/> <myXsltTweets/>
    Since the stylesheet does not use any DataPower extension functions, you can use saxon-6.5.5
    to process the stylesheet without DataPower (as mentioned in yesterday's Blog posting):
    $ saxon-6.5.5 placeholder.xml placeholder.xsl | tidy -iq -xml
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <env:Body>
        <dp:request
        xmlns:dp="http://www.datapower.com/schemas/management"
        domain="domain_1">
          <dp:get-status class="class_1" />
        </dp:request>
      </env:Body>
    </env:Envelope>
     
    $
    


     
    Hermann <myXsltBlog/> <myXsltTweets/>
    Updated on 2014-03-25T02:40:46Z at 2014-03-25T02:40:46Z by iron-man
  • Doyler86
    Doyler86
    91 Posts

    Re: Beginner XSLT Help

    ‏2013-02-07T18:54:02Z  
    • HermannSW
    • ‏2013-02-07T18:48:03Z
    Since the stylesheet does not use any DataPower extension functions, you can use saxon-6.5.5
    to process the stylesheet without DataPower (as mentioned in yesterday's Blog posting):
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">$ saxon-6.5.5 placeholder.xml placeholder.xsl | tidy -iq -xml <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <env:Body> <dp:request xmlns:dp="http://www.datapower.com/schemas/management" domain="domain_1"> <dp:get-status class="class_1" /> </dp:request> </env:Body> </env:Envelope> $ </pre>

     
    Hermann <myXsltBlog/> <myXsltTweets/>
    I use dp:encode later on in the stylesheet, before shooting off my soma request, so I am utilizing your Coproc2 + Eclipse setup that you posted sometime back for testing on my 3.8.2 box.

    thanks,

    Doyle