List attributes and elements

When writing XSL/XSLT, knowing the available fields is fairly critical. One approach is to know everything about the objects/data being provided. I prefer to have XSL show me the names. Thankfully, ther'es a real easy template that can be thrown in, that can provide this.

Short and simple, it matches the data (it may be '/', SharePoint's DataGrid web part likes to use '/dsQueryResponse/Rows/Row')... once it has the data element, the for-each loop against @* matches any available data fields, and name() displays its name.
Knowing the available data fields, I can do a simple <value-of select="@FieldName" /> and not worry that I'm guessing.
Code Snippet

<?xml version="1.0" encoding="utf-8" ?>


<xsl:stylesheet version="1.0" exclude-result-prefixes="rs z o s ddwrt dt msxsl"














  <xsl:template match="/dsQueryResponse/Rows">

    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>

    <xsl:variable name="RowCount" select="count($Rows)" />


    <table width="100%" style="padding: 5px 5px 5px 5px;">



          <xsl:for-each select="Row">

xsl:for-each select="*">

              / <xsl:value-of select="name()" /> <br />


            <xsl:for-each select="@*">

              @ <xsl:value-of select="name()" /> <br />


            <hr style="color: red" />





    Total: <xsl:value-of select="$RowCount" />



Created 2012-02-11
comments powered by Disqus