xsltumbracoumbraco-contour

Get nested record on page


Im new with XML and XSLT and wonder if any one can help here:

I have an XML form with the below data

<?xml version="1.0" encoding="utf-8"?> 
<uformrecords> 
  <uformrecord> 
    <state>Approved</state> 
    <created>2009-11-13T10:01:55</created> 
    <updated>2009-11-13T10:01:55</updated> 
    <id>119ecc43-df79-46e1-9020-b2e27e239175</id> 
    <ip>127.0.0.1</ip> 
    <pageid>0</pageid> 
    <memberkey></memberkey> 
    <fields> 
      <name record="119ecc43-df79-46e1-9020-b2e27e239175" sortorder="0"> 
        <key>2295187e-0345-4260-a406-eabcc1e774e2</key> 
        <fieldKey>e6157c93-0b54-4415-b7ba-5c7c2c953b70</fieldKey> 
        <caption>Name</caption> 
        <datatype>String</datatype> 
        <values> 
          <value><![CDATA[My Name]]></value>
        </values>
      </name>
      <email record="119ecc43-df79-46e1-9020-b2e27e239175" sortorder="1"> 
        <key>a92875a8-938d-4ba0-990a-59a3518ce62c</key> 
        <fieldKey>d8b10ffb-c437-4a44-8df6-01e6af5ac26f</fieldKey> 
        <caption>Email</caption> 
        <datatype>String</datatype> 
        <values> 
          <value><![CDATA[pph@testdomain.com]]></value> 
        </values> 
      </email> 
    </fields>
  </uformrecord>
</uformrecords>

On the page i have the below XSLT code in order to get some records

<ul> <xsl:for-each select="umbraco.contour:GetRecordsFromPage($currentPage/@id)//uformrecord"> 
    <xsl:sort select="created" order="ascending"/> 
    <li> 
        A record with id id <xsl:value-of select="id"/> with the state set to <xsl:value-of select="state"/> i 
        was created on <xsl:value-of select="umbraco.library:LongDate(created)"/> 
    </li> 

    </xsl:for-each> 
</ul>

This works fine but if i add caption

<xsl:value-of select="caption"/>

the field is empty. I think its empty as the node may need to be iterated through. So i have the below code to iterate through the records

<xsl:for-each select="umbraco.contour:GetRecord($id)/uformrecord/fields/child::*"> 
    <xsl:sort select="caption" order="ascending"/> 
    <h4> 
        <xsl:value-of select=" caption"/> 
    </h4> 
</xsl:for-each>

but i have the error that ID is not declared - if i declare id i get the error that its not in the proper GUID format. Could anyone advise how to get over this problem and show the "caption" element?

Thanks


Solution

  • In your first xsl:for-each you are iterating over uformrecord, so that will be your context within the loop.

    <xsl:for-each select="umbraco.contour:GetRecordsFromPage($currentPage/@id)//uformrecord"> 
    

    However, as you spotted, the caption element is not an immediate child of the uformrecord element, but is lower down under field elements.

    As you are already positioned on an uformrecord, you should be able to simplify your xsl:for-each to this:

    <xsl:for-each select="fields/*"> 
        <xsl:sort select="caption" order="ascending"/> 
        <h4> 
            <xsl:value-of select="caption"/> 
        </h4> 
    </xsl:for-each>