Listing XML nodes in XSLT

How to show the name/value of all nodes in an XML element using XSLT

Occassionally you don't know what XML data is being passed through to XSLT for process, so the following can be used to quickly show the data...

<xsl:for-each select="//mydata/*">
  <xsl:value-of select="name()"/> = <xsl:value-of select="."/><br/>
</xsl:for-each>

And if you need the attributes, simply add a @ character...

<xsl:for-each select="//mydata/@*">
  <xsl:value-of select="name()"/> = <xsl:value-of select="."/><br/>
</xsl:for-each>
Added 17/05/2022 15:32

XML node value in TSQL

Better way of getting text from XML node in SQL Server

For years I've used the a simple . to get the text value from an XML node in SQL Server (in this case converting to integer)...

SELECT X.value('.','int')
FROM @XML.nodes('/data/id') AS X(X)

However, I've recently been informed that it's much better performance to use text()[1] instead...

SELECT X.value('text()[1]','int')
FROM @XML.nodes('/data/id') AS X(X)
Added 16/05/2022 11:31

Class-less JSON (Newtonsoft)

How to general valid JSON with Newtonsoft without a class

Newtonsoft gives quick and easy way to generate JSON by passing a serialisable class...

Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(myClassObj)

But there are times when you just want to create arbitary JSON data without the need to create a whole new class just for the purpose. For this we can use Newtonsoft.Json.Linq.JObject

Dim jObj As New Newtonsoft.Json.Linq.JObject() From {
  {"attr1", "First Attribute"},
  {"attr2", false}
}
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(jObj)

And if you need an array, use Newtonsoft.Json.Linq.JArray

Dim jObj As New Newtonsoft.Json.Linq.JArray()
jObj.Add(New Newtonsoft.Json.Linq.JObject() From {{"attr1", "First Attribute"}, {"attr2", false}})
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(jObj)

Added 06/05/2022 10:40