I have XSLT that creates HTML from XML. There are several parts of the resulting HTML that I need to create by external C# method (extension method).
How do I embed this C# method into XSLT (within what xsl nodes? I do know how to use common extension functions inside XSLT)
What should my C# function return? HTML-formatted string? With escaped HTML tags or without?
Thank you very much!
Well if you want to return HTML tag soup not conforming to XML rules then you can only return a string and then use e.g. <xsl:value-of select="pf:yourFunction()" disable-output-escaping="yes"/>. If you want to build a node-set of nodes or a result tree fragment then check the documentation, it shows the mapping between the XSLT types and the .NET framework types e.g. if you want your function to return an result that XSLT sees as a node-set then use the type XPathNodeIterator as the .NET return type of your extension function.
Related
I want to transform my Xml with a Xslt file. Can I access to the Xml that is transforming with Xslt in an embedded code piece using C#.
<![CDATA[
public string GetSomething(string path)
{
// Get the whole Xml that is transforming
// And do something with it
// return result
}
]]>
<xsl:value-of select ="GetSomething(courier:_appointment)"/>
Is it possible?
Thanks in advance,
There is some scripting support inside xslt, via <msxsl:script>, however: it is probably a better idea to use an extension object. Basically, you write a regular C# object (although you need to mark it as COM-callable, IIRC), and add it via XsltArgumentList, in particular AddExtensionObject - and in the process associate it with a particular urn. Your xslt then declares an xml namespace-alias for the url (i.e. xmlns:myExtension="blah blah"), and uses myExtension:someMethod(...) in the code.
There is a full example on MSDN.
Is there anyway to check with C# code , to see if an XSLT expects parameters?
XSLT is XML, so you can load the file into a XDocument and query it to see if there are any parameter elements defined on the top element.
Evaluate this XPath expression against the XML document that contains the XSLT file:
/*/xsl:param
If the result contains at least one node, then the xslt stylesheet contains at least one global xsl:param -- and the main purpose of a global xsl:param is to be set externally by the invoker of the transformation.
where you have to add the XSLT namespace and associate it with the "xsl:" prefix using an XmlNamespaceManager object.
Like the statement,
string value = document.forms["sap.client.SsrClient.form"].elements["sapwdssr..requestCounter"].value;
in javascript, is there a corresponding statement to get the value of a particular input element within a particular form in C#?
I can do so by using HTMLDocument and mshtml interface. But that is a rather cumbersome process so if any direct method or property exists it would be great.
I assume you are asking to parse HTML, rather than attempting to do some form of runtime manipulation of a rendered web page, correct?
If that's the case, I highly suggest you look into the HTML Agility Pack, which we have used very successfully to parse HTML as if it were XML. You could do your stuff with a simple XPath query.
Is it possible to use variables like <%=person.LastName %> in XML string this way?
XElement letters = new XElement("Letters");
XElement xperson = XElement.Parse("<Table><Row><Cell><Text>
<Segment>Dear <%=person.Title%> <%=person.FirstName%> <%=person.LastName%>,
</Segment></Text></Cell></Row>...");
foreach (Person person in persons){
letters.Add(xperson)
}
If it's possible, it would be a lifesaver since I can't use XElement and XAttribute to add the nodes manually. We have multiple templates and they frequently change (edit on the fly).
If this is not doable, can you think of another way so that I can use templates for the XML?
Look like it's possible in VB
http://aspalliance.com/1534_Easy_SQL_to_XML_with_LINQ_and_Visual_Basic_2008.6
This is an exclusive VB.NET feature known as XML Literals. It was added in VB 9.0. C# does not currently support this feature. Although Microsoft has stated its intent to bridge the gap between the languages in the future, it's not clear whether this feature will make it to C# any time soon.
Your example doesn't seem clear to me. You would want to have the foreach loop before parsing the actual XML since the values are bound to the current Person object. For example, here's a VB example of XML literals:
Dim xml = <numbers>
<%= From i In Enumerable.Range(1, 5)
Select <number><%= i %></number>
%>
</numbers>
For Each e In xml.Elements()
Console.WriteLine(e.Value)
Next
The above snippet builds the following XML:
<numbers>
<number>1</number>
<number>2</number>
<number>3</number>
</numbers>
Then it writes 1, 2, 3 to the console.
If you can't modify your C# code to build the XML dynamically then perhaps you could write code that traverses the XML template and searches for predetermined fields in attributes and elements then sets the values as needed. This means you would have to iterate over all the attributes and elements in each node and have some switch statement that checks for the template field name. If you encounter a template field and you are currently iterating attributes you would set it the way attributes are set, whereas if you were iterating elements you would set it the way elements are set. This is likely not the most efficient approach but is one solution.
The simplest solution would be to use VB.NET. You can always develop it as a stand-alone project, add a reference to the dll from the C# project, pass data to the VB.NET class and have it return the final XML.
EDIT: to clarify, using VB.NET doesn't bypass the need to update the template. It allows you to specify the layout easier as an XML literal. So code still needs to be updated in VB once the layout changes. You can't load an XML template from a text file and expect the fields to be bound that way. For a truly dynamic solution that allows you to write the code once and read different templates my first suggestion is more appropriate.
I am looking for a liberal xml parser, one for java and one for C#, that can "properly" parse
unquoted attribute values
non-conjugated attribute
like:
<Person id=candy female single phone=555.666.7777 />
rather than
<Person id='candy' female='true' single='true' phone='555.666.7777'/>
jericho html parser in java can handle non-validating xml. nekohtml and jtidy are also available. there may be some methods to output back a validating doc.
in c#, you might be able to use Tidy.Net since the markup resembles loose html, or .NET's native HTMLDocumentClass