XSLT – How to keep only wanted elements from XML

This general transformation: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:ns=”some:ns”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:strip-space elements=”*”/> <ns:WhiteList> <name>ns:currency</name> <name>ns:currency_code3</name> </ns:WhiteList> <xsl:template match=”node()|@*”> <xsl:copy> <xsl:apply-templates select=”node()|@*”/> </xsl:copy> </xsl:template> <xsl:template match= “*[not(descendant-or-self::*[name()=document(”)/*/ns:WhiteList/*])]”/> </xsl:stylesheet> when applied on the provided XML document (with namespace definition added to make it well-formed): <ns:stuff xmlns:ns=”some:ns”> <ns:things> <ns:currency>somecurrency</ns:currency> <ns:currency_code/> <ns:currency_code2/> <ns:currency_code3/> <ns:currency_code4/> </ns:things> </ns:stuff> produces the wanted … Read more

.SelectSingleNode in Powershell script using xPath not working on extracting values from web.config file

Your XML file has a namespace: <configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0″> so you need a namespace manager for SelectSingleNode (see section “Remarks”): XPath expressions can include namespaces. Namespace resolution is supported using the XmlNamespaceManager. If the XPath expression includes a prefix, the prefix and namespace URI pair must be added to the XmlNamespaceManager. Something like this should work: … Read more

How do I create an XPath function in Groovy

You can do something like this: import javax.xml.xpath.* import javax.xml.parsers.DocumentBuilderFactory def testxml=””‘ <records> <car name=”HSV Maloo” make=”Holden” year=”2006″> <country>Australia</country> <record type=”speed”>Production Pickup Truck with speed of 271kph</record> </car> </records> ”’ def processXml( String xml, String xpathQuery ) { def xpath = XPathFactory.newInstance().newXPath() def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() def inputStream = new ByteArrayInputStream( xml.bytes ) def records … Read more

Is it possible to apply normalize-space to all nodes XPath expression finds?

Using XPath “/html/body/table/tr/td/text()” we will get [” Item 1″, ” Item 2″]. Is it possible to trim white space for example using normalize-space() function to get [“Item 1”, “Item 2”]? Not in XPath 1.0. In Xpath 2.0 this is simple: /html/body/table/tr/td/text()/normalize-space(.) In XPath 2.0 a location step of an XPath expression may be a function … Read more