예제 #1
0
파일: XsltModule.cs 프로젝트: ruo2012/myxsl
        public XPathItem Compile(XPathItem stylesheet, string processor)
        {
            CompiledStylesheetReference reference;

            if (stylesheet.IsNode)
            {
                IXsltProcessor proc = (processor != null) ?
                                      Processors.Xslt[processor]
               : this.CurrentXsltProcessor ?? Processors.Xslt.DefaultProcessor;

                int hashCode;

                XsltInvoker.With((XPathNavigator)stylesheet, proc, null, out hashCode);

                if (processor == null)
                {
                    return(this.ItemFactory.CreateAtomicValue(hashCode, XmlTypeCode.Integer));
                }

                reference = new CompiledStylesheetReference {
                    HashCode  = hashCode,
                    Processor = processor
                };
            }
            else
            {
                Uri stylesheetUri = StylesheetAsUri(stylesheet);

                if (processor == null ||
                    processor == Processors.Xslt.Default)
                {
                    return(this.ItemFactory.CreateAtomicValue(stylesheetUri.ToString(), XmlTypeCode.String));
                }

                XsltInvoker.With(stylesheetUri, processor);

                reference = new CompiledStylesheetReference {
                    Uri       = stylesheetUri.AbsoluteUri,
                    Processor = processor
                };
            }

            return(this.ItemFactory
                   .CreateDocument(reference)
                   .CreateNavigator());
        }
예제 #2
0
파일: XsltModule.cs 프로젝트: ruo2012/myxsl
        XPathNavigator ExecuteStylesheet(XPathItem stylesheet, XsltRuntimeOptions options)
        {
            XsltInvoker invoker;

            IXsltProcessor currentOrDefaultProc = this.CurrentXsltProcessor ?? Processors.Xslt.DefaultProcessor;

            if (stylesheet.IsNode)
            {
                XPathNavigator node = ((XPathNavigator)stylesheet).Clone();

                if (node.NodeType == XPathNodeType.Root)
                {
                    node.MoveToChild(XPathNodeType.Element);
                }

                if (node.NodeType != XPathNodeType.Element)
                {
                    throw new ArgumentException("if stylesheet is a node() it must be either a document-node(element()) or an element() node.", "stylesheet");
                }

                if (node.NamespaceURI == Namespace)
                {
                    XmlSerializer serializer = XPathItemFactory.GetSerializer(typeof(CompiledStylesheetReference));

                    var reference = (CompiledStylesheetReference)serializer.Deserialize(node.ReadSubtree());

                    IXsltProcessor specifiedProcessor = (reference.Processor != null) ?
                                                        Processors.Xslt[reference.Processor]
                  : null;

                    invoker = (reference.HashCode > 0) ?
                              XsltInvoker.With(reference.HashCode, specifiedProcessor ?? currentOrDefaultProc)
                  : XsltInvoker.With(reference.Uri, specifiedProcessor);
                }
                else
                {
                    invoker = XsltInvoker.With((XPathNavigator)stylesheet, currentOrDefaultProc);
                }
            }
            else
            {
                object value = stylesheet.TypedValue;

                if (value.GetType().IsPrimitive)
                {
                    int hashCode = Convert.ToInt32(value, CultureInfo.InvariantCulture);

                    invoker = XsltInvoker.With(hashCode, currentOrDefaultProc);
                }
                else
                {
                    Uri stylesheetUri = StylesheetAsUri(stylesheet);

                    invoker = XsltInvoker.With(stylesheetUri);
                }
            }

            return(invoker.Transform(options)
                   .Result()
                   .CreateNavigator());
        }