Programatically Read XSL Params

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aday
    New Member
    • Dec 2008
    • 5

    Programatically Read XSL Params

    is xpath the best way to programicly read the params an xsl page requires or is there a better way?

    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I think there's a misunderstandin g. XPath is the/one language to access elements in an xml document tree. and <xsl:param> is a part of the XSLT language. you can't read <xsl:param> by XPath, though you can use them inside XPath. (well, technicly it should be possible but XSLT provides easier access - you read them in XSLT by calling the parameter name with a prefixed $ sign: $param_name (like variables in PHP))

    Comment

    • aday
      New Member
      • Dec 2008
      • 5

      #3
      thanks

      but I am not sure I understand..... ..

      I am writing a winforms program in C# (3.5 framework) and I need to read the params from am xsl doc and present them to the user to fill in the required values.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        as far as I understand XSLT, params (in the form of <xsl:param> are for the internal use in the stylesheet. usually a xsl stylesheet is used to transform one xml into another xml (or html, or text). so what the xsl contains is out of interest as long as it does its job.

        out of interest, what is your xsl doc supposed to do, maybe we're talking about different things.

        regards

        Comment

        • aday
          New Member
          • Dec 2008
          • 5

          #5
          basicly I am building a Code and SQL Stored Procedure generation tool (vs addin) for vs.net all my templates are xsl docs that users can create and add. And my xml data is generated using the SqlXmlCommand in vs.net

          Say a user has created a template (xsl doc) that accepts a param called "postfix" and "prefix" that will be used in the transformation.

          I would like to be able to read the params that are defined in the xsl doc so I can then request the value from the user using the tool for the values.

          I hope this explanes it .

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you make me curious. your users can write xsl from scratch? maybe you should post some code, because I still think we're talking about different things.

            you can get the parameter names via DOM, since xsl is xml. XPath is possible too, if you load the xsl into an xml reader (don't know what C# provides). It really depends on how the xsl is created... there might be more and easier ways.

            regards
            Last edited by Dormilich; Dec 3 '08, 09:32 PM. Reason: got an idea

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              This reminds me of the old javascript way of running xsl.
              In order to set parameters, you would actually have to change the values of the parameters of the elements in DOM of the xsl, then run the transformation.

              Anyways, I think you can use SelectNodes("//xsl:template[@match]/xsl:param/@name", nsmgr)
              where nsmgr is your namespace manager, and has the namespace xsl defined.

              Assumptions:
              You have a main template which uses match.
              When you call your other matched templates, you don't use parameters; (Usually the case.)

              Comment

              • aday
                New Member
                • Dec 2008
                • 5

                #8
                I have added a sample xsl doc.

                Here is an example of me setting the param in c#

                [code=c#]

                //Initialize
                string returnValue = string.Empty;
                XslCompiledTran sform xslDoc = new XslCompiledTran sform();
                XmlUrlResolver xslResolver = new XmlUrlResolver( );
                XsltArgumentLis t xslArg = new XsltArgumentLis t();

                //Load XSL Template
                xslDoc.Load(Ful lTemplateName);

                //Get Table Columns In XML
                XmlReader xmlColumnList = GetDatabaseTabl eColumnList(ddl Databases.Text, TableName);

                //xmlColumnList.M oveToContent();
                TextWriter swTrasformation Output = new StringWriter();

                //Set Required Arguments
                xslArg.AddParam ("spPrefix", "", txtPrefix.Text) ;

                //Do Transformation
                xslDoc.Transfor m(xmlColumnList , xslArg, swTrasformation Output);
                swTrasformation Output.Flush();

                return swTrasformation Output.ToString ();

                [/code]
                Attached Files

                Comment

                • aday
                  New Member
                  • Dec 2008
                  • 5

                  #9
                  Thanks, I will try this.... I assume you are doing this with XPath

                  Originally posted by jkmyoung
                  This reminds me of the old javascript way of running xsl.
                  In order to set parameters, you would actually have to change the values of the parameters of the elements in DOM of the xsl, then run the transformation.

                  Anyways, I think you can use SelectNodes("//xsl:template[@match]/xsl:param/@name", nsmgr)
                  where nsmgr is your namespace manager, and has the namespace xsl defined.

                  Assumptions:
                  You have a main template which uses match.
                  When you call your other matched templates, you don't use parameters; (Usually the case.)

                  Comment

                  Working...