public override void TreeToTreeTransform() { if (sourceDocument != null) { XdmNavigator contextItem = sourceDocument.CreateNavigator(); var documentSet = new DocumentSet(nameTable, stylesheet.InputSettings, schemaAware ? documentResolverSchemaAware : documentResolver, null, resourceResolver); DynamicContextSettings settings = new DynamicContextSettings(); settings.ContextItem = contextItem; settings.DocumentSet = documentSet; //DynamicContextSettings settings = new DynamicContextSettings { ContextItem = contextItem }; using (XdmDocumentWriter writer = XdmDocumentWriter.Create()) { stylesheet.ApplyTemplates(settings, writer); resultDocument = writer.Document; } } else { var documentSet = new DocumentSet(nameTable, stylesheet.InputSettings, documentResolver, null, resourceResolver); DynamicContextSettings settings = new DynamicContextSettings { DocumentSet = documentSet }; XmlQualifiedName qname = new XmlQualifiedName("main"); using (XdmDocumentWriter writer = XdmDocumentWriter.Create()) { stylesheet.CallTemplate(qname, settings, writer); resultDocument = writer.Document; } } }
public override void FileToFileTransform(Uri sourceUri, string resultFileLocation) { if (sourceUri != null) { using (XmlReader reader = XmlReader.Create(sourceUri.ToString(), schemaAware ? xmlReaderSettingsSchemaAware : xmlReaderSettings)) { document = new XdmDocument(reader, XmlSpace.Preserve); reader.Close(); } XdmNavigator contextItem = document.CreateNavigator(); var documentSet = new DocumentSet(nameTable, stylesheet.InputSettings, schemaAware ? documentResolverSchemaAware : documentResolver, null, resourceResolver); DynamicContextSettings settings = new DynamicContextSettings(); settings.ContextItem = contextItem; settings.DocumentSet = documentSet; //DynamicContextSettings settings = new DynamicContextSettings { ContextItem = contextItem }; stylesheet.SerializationSettings.CloseOutput = true; TextWriter writer = new StreamWriter(resultFileLocation); stylesheet.ApplyTemplates(settings, writer); writer.Close(); } else { var documentSet = new DocumentSet(nameTable, stylesheet.InputSettings, documentResolver, null, resourceResolver); DynamicContextSettings settings = new DynamicContextSettings { DocumentSet = documentSet }; stylesheet.SerializationSettings.CloseOutput = true; XmlQualifiedName qname = new XmlQualifiedName("main"); TextWriter writer = new StreamWriter(resultFileLocation); stylesheet.CallTemplate(qname, settings, writer); writer.Close(); } this.resultFile = resultFileLocation; }
public void Transform() { var nameTable = new NameTable(); var xmlReaderSettings = new XmlReaderSettings { NameTable = nameTable }; XdmDocument document; using (var reader = XmlReader.Create(_rootPath + "\\XmlPrimeXsltTransformer\\data.xml", xmlReaderSettings)) { document = new XdmDocument(reader); } var xsltSettings = new XsltSettings(nameTable) { ContextItemType = XdmType.Node }; var xslt = Xslt.Compile(_rootPath + "\\XmlPrimeXsltTransformer\\template.xsl", xsltSettings); var contextItem = document.CreateNavigator(); var settings = new DynamicContextSettings { ContextItem = contextItem }; using (var outputStream = File.Create(outputPath)) xslt.ApplyTemplates(settings, outputStream); }
/** * Test that the result of the transformation satisfies a given assertion * @param assertion the assertion, in the form of an XPath expression which * must evaluate to TRUE when executed with the transformation * result as the context item * @return the result of testing the assertion */ public override bool TestAssertion(String assertion) { bool DocOK = true; bool FileOK = true; if (_resultDocument != null) { XPathNavigator navigator = _resultDocument.CreateNavigator(); DocOK = (bool)navigator.Evaluate(XPathExpression.Compile(assertion)); } if (resultFile != null) { XPathDocument resultDoc = new XPathDocument(resultFile); XPathNavigator navigator = resultDoc.CreateNavigator(); FileOK = (bool)navigator.Evaluate(XPathExpression.Compile(assertion)); } return(DocOK && FileOK); }
public override bool TestAssertion(string assertion) { bool DocOK = true; bool FileOK = true; if (resultDocument != null) { XPathSettings xpathSettings = new XPathSettings(nameTable) { ContextItemType = XdmType.Node }; var xpath = XPath.Compile(assertion, xpathSettings); var contextItem = resultDocument.CreateNavigator(); var settings = new DynamicContextSettings { ContextItem = contextItem }; DocOK = xpath.EvaluateToItem(contextItem).ValueAsBoolean; } if (resultFile != null) { XdmDocument resultDoc; using (var reader = XmlReader.Create(resultFile, xmlReaderSettings)) { resultDoc = new XdmDocument(reader); reader.Close(); } XPathSettings xpathSettings = new XPathSettings(nameTable) { ContextItemType = XdmType.Node }; var xpath = XPath.Compile(assertion, xpathSettings); var contextItem = resultDoc.CreateNavigator(); var settings = new DynamicContextSettings { ContextItem = contextItem }; FileOK = xpath.EvaluateToItem(contextItem).ValueAsBoolean; } return(DocOK && FileOK); }
private static void PerformTransformation(string inputPath, string xsltPath, string outputPath) { Debug.Assert(inputPath != null, "inputPath in null"); Debug.Assert(xsltPath != null, "xsltPath in null"); Debug.Assert(inputPath != null, "outputPath in null"); // First, we create a new XmlNameTable instance. This will be used to share information such // as element and attribute names between the XML documents and the transformation. var nameTable = new NameTable(); // Next we create an XmlReaderSettings instance and set its NameTable property. // In order for XmlPrime to work correctly all documents passed in to Xslt must be loaded // with the XmlNameTable used to compile the query. var xmlReaderSettings = new XmlReaderSettings { NameTable = nameTable }; // In order to transform the document we load it into an XdmDocument. XdmDocument document; using (var reader = XmlReader.Create(inputPath, xmlReaderSettings)) { document = new XdmDocument(reader); } // In order to describe how the transformation should be compiled we need set up an XsltSettings // object. This describes all the settings used for compilation. // In particular, we will set the name table used by the transformation to match the one we used // earlier and we will set the context item type. // By default the context item type is set to none, and so the context item cannot be set unless // we override the type here. var xsltSettings = new XsltSettings(nameTable) { ContextItemType = XdmType.Node, }; // We can then compile the transformation using the Compile method. // This returns us an Xslt object encapsulating the transformation. var xslt = Xslt.Compile(xsltPath, xsltSettings); // Now we have our transformation object we now just need to execute it. // We use a DynamicContextSettings object which describes the parameters used to evaluate the query. // In particular we will set the context item to be the document that we loaded earlier. var contextItem = document.CreateNavigator(); //ParameterDictionary parameters=new ParameterDictionary(); //parameters.Add(new XmlQualifiedName("titleStyleId"),2); var settings = new DynamicContextSettings { ContextItem = contextItem }; settings.Parameters.Add(new XmlQualifiedName("titleStyleId"), 2); // We will use the ApplyTemplates method to initiate a transformation by applying templates // in the default mode and serializing the primary result document to a stream. using (var outputStream = File.Create(outputPath)) { xslt.ApplyTemplates(settings, outputStream); } }
/// <summary> /// Performs the transformation. /// </summary> /// <param name="inputPath">The filename of the context item document.</param> /// <param name="xsltPath">The filename of the XSLT transformation.</param> /// <param name="outputPath">The filename of the primary output.</param> private static void PerformTransformation(string inputPath, string xsltPath, string outputPath) { Debug.Assert(inputPath != null, "inputPath in null"); Debug.Assert(xsltPath != null, "xsltPath in null"); Debug.Assert(inputPath != null, "outputPath in null"); // First, we create a new XmlNameTable instance. This will be used to share information such // as element and attribute names between the XML documents and the transformation. var nameTable = new NameTable(); // Next we create an XmlReaderSettings instance and set its NameTable property. // In order for XmlPrime to work correctly all documents passed in to Xslt must be loaded // with the XmlNameTable used to compile the query. var xmlReaderSettings = new XmlReaderSettings { NameTable = nameTable }; // In order to transform the document we load it into an XdmDocument. XdmDocument document; using (var reader = XmlReader.Create(inputPath, xmlReaderSettings)) { document = new XdmDocument(reader); } // In order to describe how the transformation should be compiled we need set up an XsltSettings // object. This describes all the settings used for compilation. // In particular, we will set the name table used by the transformation to match the one we used // earlier and we will set the context item type. // By default the context item type is set to none, and so the context item cannot be set unless // we override the type here. var xsltSettings = new XsltSettings(nameTable) { ContextItemType = XdmType.Node }; // We can then compile the transformation using the Compile method. // This returns us an Xslt object encapsulating the transformation. var xslt = Xslt.Compile(xsltPath, xsltSettings); // Now we have our transformation object we now just need to execute it. // We use a DynamicContextSettings object which describes the parameters used to evaluate the query. // In particular we will set the context item to be the document that we loaded earlier. var contextItem = document.CreateNavigator(); var settings = new DynamicContextSettings { ContextItem = contextItem }; // We will use the ApplyTemplates method to initiate a transformation by applying templates // in the default mode and serializing the primary result document to a stream. using (var outputStream = File.Create(outputPath)) { xslt.ApplyTemplates(settings, outputStream); } // NOTE: We could just have used xslt.ApplyTemplates(contextItem, outputStream) in this simple case. }