コード例 #1
0
        internal static QName GetQNameAttribute(XPathCompiler xpath, XdmItem contextItem, string attributePath)
        {
            string exp = "for $att in " + attributePath +
                         " return if (contains($att, ':')) then resolve-QName($att, $att/..) else " +
                         " if (contains($att,'{')) then QName(substring-before(substring-after($att,'{'),'}'),substring-after($att,'}')) else" +
                         " QName('', $att)";
            XdmAtomicValue qname = (XdmAtomicValue)xpath.EvaluateSingle(exp, contextItem);

            return(qname == null ? null : new QName(qname.ToString()));
        }
コード例 #2
0
    /**
     * Show how to get stylesheets that are associated with a given
     * xml document via the xml-stylesheet PI (see http://www.w3.org/TR/xml-stylesheet/).
     */
    public static void ExampleUseAssociated(String sourceUri)
    {
        // Create a Processor instance.
        Processor      processor = new Processor();
        XsltExecutable exec;

        // Load the source document
        XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri));

        Console.WriteLine("=============== source document ===============");
        Console.WriteLine(input.OuterXml);
        Console.WriteLine("=========== end of source document ============");

        // Navigate to the xml-stylesheet processing instruction having the pseudo-attribute type=text/xsl;
        // then extract the value of the href pseudo-attribute if present

        String path = @"/processing-instruction(xml-stylesheet)[matches(.,'type\s*=\s*[''""]text/xsl[''""]')]" +
                      @"/replace(., '.*?href\s*=\s*[''""](.*?)[''""].*', '$1')";

        XPathSelector eval = processor.NewXPathCompiler().Compile(path).Load();

        eval.ContextItem = input;
        XdmAtomicValue hrefval = (XdmAtomicValue)eval.EvaluateSingle();

        Console.WriteLine("evaluated");
        String href = hrefval.ToString();

        if (href == null || href == "")
        {
            Console.WriteLine("No suitable xml-stylesheet processing instruction found");
            return;
        }
        else if (href[0] == '#')
        {
            // The stylesheet is embedded in the source document and identified by a URI of the form "#id"

            Console.WriteLine("Locating embedded stylesheet with href = " + href);
            String idpath = "id('" + href.Substring(1) + "')";
            eval             = processor.NewXPathCompiler().Compile(idpath).Load();
            eval.ContextItem = input;
            XdmNode node = (XdmNode)eval.EvaluateSingle();
            if (node == null)
            {
                Console.WriteLine("No element found with ID " + href.Substring(1));
                return;
            }
            exec = processor.NewXsltCompiler().Compile(node);
        }
        else
        {
            // The stylesheet is in an external document

            Console.WriteLine("Locating stylesheet at uri = " + new Uri(input.BaseUri, href));

            // Fetch and compile the referenced stylesheet
            exec = processor.NewXsltCompiler().Compile(new Uri(input.BaseUri, href.ToString()));
        }

        // Create a transformer
        XsltTransformer transformer = exec.Load();

        // Run it
        transformer.SetParameter(new QName("", "", "a-param"), new XdmAtomicValue("hello to you!"));
        transformer.InitialContextNode = input;
        XdmDestination results = new XdmDestination();

        transformer.Run(results);
        Console.WriteLine("1: " + results.XdmNode.StringValue);
    }