How to retrieve global parameters with .NET ?

tg
2010-04-22
2012-10-08
  • tg
    tg
    2010-04-22

    Hi,

    how can I retrieve global parameters set in a stylesheet?

    for example:

    I created a windows form where I can set Parameters. these values are later
    overgiven with SetParameter (~"parameter_name", "test"). that works fine. But
    Id like to parse the stylesheet before and autofill the form with the
    parameters set in the stylesheet. Just to make it easier and more comfortable
    for the user. I hope its clear what I mean. sry for my bad english.

    I found some solutions on that for java. but not for c#. maybe someone has an
    idea?

    greetings

     
  • tg
    tg
    2010-04-22

    sry this should be in "help" not in open discussion ...

    to make my post clearer. I mean something like this:

    XsltTransformer atransformer;
    string params = atransformer.GetParameters();

    greetings

     
  • Michael Kay
    Michael Kay
    2010-04-22

    The XsltExecutable in Saxon.Api unfortunately doesn't give you any access to
    this information. However you can call Load() to get an XsltTransformer, and
    on this you can get the Java net.sf.saxon.Controller as the value of the
    Implementation property. Although this is a Java-derived object, you can
    manipulate it as any .NET object, though the style may be a little unfamiliar,
    e.g. method names beginning lower-case. You will have to add a reference to
    the saxon9he.dll (or -pe or -ee) to your application.

    From the Controller you can do getExecutable().getCompiledGlobalVariables()
    which is a (Java) HashMap from names to variable definitions; for a global
    parameter (as distinct from a Variable) the value will be of class
    net.sf.saxon.instruct.GlobalParam. Sorry that's a bit circuitous, it's the
    best I can come up with at the moment.

     
  • tg
    tg
    2010-04-22

    thanks mr kay =)
    that works fine

     
  • tg
    tg
    2010-04-27

    its me again.

    well, as I said it seems to work fine. but now I have a problem. :-/ when I
    have a param like this:




    I get this:

    DocumentInstr(ValueOf(SimpleContentConstructor(((document($path,
    .)/child::element(config, xs:anyType))/child::element(examplXML, xs:anyType)),
    " ")))

    which is not good. it works great with params written in normal text but when
    I write a param with xslt expressions he tries to interpret it I guess.

    do you have an idea how to solve this?

     
  • Michael Kay
    Michael Kay
    2010-04-27

    Don't simply do toString() on the value of the select expression. The user
    interface is up to you, but I would say: if the select expression is a an
    instance of Literal, display its string value; otherwise display something
    like "(computed value)". The original text of the expression from the source
    stylesheet is not available in the compiled code.

     
  • Michael Kay
    Michael Kay
    2010-04-27

    Incidentally, the compiled code would be much less convoluted if you wrote the
    xsl:param like this:


    Using xsl:value-of in this kind of situation is really bad coding, but
    unfortunately very common: it forces saxon to read the document and then make
    a copy of it.

     
  • tg
    tg
    2010-04-28

    I decided to retreive the params on another way. I parse the source xslt file
    and write the given values into a string, and put them in a list. and so on.

    basically like this:
    XmlReader xmlReader = null;
    xmlReader = XmlReader.Create(xsltfile);
    while (xmlReader.Read())
    {
    string param, value = "";
    if (xmlReader.Name == "xsl:param" && xmlReader.IsStartElement() &&
    xmlReader.Depth == 1)
    {

    bool select = false;
    param = xmlReader.GetAttribute("name");

    if (xmlReader.MoveToAttribute("select"))
    {

    value = xmlReader.ReadInnerXml();
    select = true;

    }

    if (xmlReader.MoveToContent() != XmlNodeType.None && select == false)
    {
    value = xmlReader.ReadInnerXml();
    value = Item.value.Trim();
    select = false;
    }

    Program.konfiguration.parameters.Add(Item);
    }
    }
    xmlReader.Close();

    works fine. and it seems to run fast.

     
  • Michael Kay
    Michael Kay
    2010-04-28

    Fine. Be aware that you aren't accessing included/imported stylesheets, and
    you're relying on the namespace prefix being "xsl". But as a cheap-and-
    cheerful solution, it's fine.

     
  • tg
    tg
    2010-04-28

    I will consider these things. thx :)

    and really thank you for your time!