XMLReader from a string content
XmlReader xmlReader = XmlReader.Create(new StringReader(YourStringValue));
XmlReader xmlReader = XmlReader.Create(new StringReader(YourStringValue));
As Filburt says; but also note that it’s usually better to write test=”not(Count=”N/A”)” If there’s exactly one Count element they mean the same thing, but if there’s no Count, or if there are several, then the meanings are different. 6 YEARS LATER Since this answer seems to have become popular, but may be a little … Read more
Using one of the most fundamental XSLT design patterns: “Overriding the identity transformation” one will just write the following: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes”/> <xsl:template match=”node()|@*”> <xsl:copy> <xsl:apply-templates select=”node()|@*”/> </xsl:copy> </xsl:template> <xsl:template match=”Element[@fruit=”apple” and @animal=”cat”]”/> </xsl:stylesheet> Do note how the second template overrides the identity (1st) template only for elements named “Element” that have an … Read more
why did the former code outputs TEXT, why should I insist XSL to ignore all other text? is that the behavior of all XML parsers or only of my own You are discovering one of the most fundamental XSLT features as specified in the Specification: the built-in templates of XSLT. From the Spec: There is … Read more
It isn’t too meaningful to give a template both a name and a mode. The name attribute fully identifies a template and there cannot be two templates with the same name and different modes. The mode attribute allows the same nodes to be processed more than once, using different modes. Here is a short example: … Read more
I love your question. You’re very articulate about what you do not yet understand. You just need something to tie things together. My recommendation is that you read “How XSLT Works”, a chapter I wrote to address exactly the questions you’re asking. I’d love to hear if it ties things together for you. Less formally, … Read more
Here is a version with configurable parameters that you can set programmatically: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output method=”text” encoding=”utf-8″ /> <xsl:param name=”delim” select=”‘,'” /> <xsl:param name=”quote” select=”‘"'” /> <xsl:param name=”break” select=”‘
'” /> <xsl:template match=”https://stackoverflow.com/”> <xsl:apply-templates select=”projects/project” /> </xsl:template> <xsl:template match=”project”> <xsl:apply-templates /> <xsl:if test=”following-sibling::*”> <xsl:value-of select=”$break” /> </xsl:if> </xsl:template> <xsl:template match=”*”> <!– remove normalize-space() if you … Read more
Using lxml, import lxml.etree as ET dom = ET.parse(xml_filename) xslt = ET.parse(xsl_filename) transform = ET.XSLT(xslt) newdom = transform(dom) print(ET.tostring(newdom, pretty_print=True))
I notice that the only “online” checker in the answers seems a bit clunky. It requires submission to a server, and it doesn’t handle namespaces properly. I thought I could do better, and that such a tool would be quite useful, so I made one. I realize it is slightly self-promoting to mention it here, … Read more
Here is sample for using java api for transformer, as @Raedwald said: import javax.xml.transform.*; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; public class TestMain { public static void main(String[] args) throws IOException, URISyntaxException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Source xslt = new StreamSource(new File(“transform.xslt”)); Transformer transformer = factory.newTransformer(xslt); Source text = … Read more