<?xml version="1.0"?>
<sx:resources xmlns:sx="http://www.servingxml.com/core"
              xmlns:msv="http://www.servingxml.com/extensions/msv"
              xmlns:saxon="http://www.servingxml.com/extensions/saxon">

  <sx:service id="countries">
    <sx:parameter name="validate">
      <saxon:xquery>
        <sx:toString>doc('data/parameters.xml')/parameters/validate
        </sx:toString>
      </saxon:xquery>
    </sx:parameter>

    <sx:serialize>
      <sx:choose>
        <sx:when test="$validate='yes'">
          <sx:transform>
            <sx:content ref="countries"/>
            <msv:schemaValidator>
              <sx:urlSource url="data/countries.xsd"/>
            </msv:schemaValidator>
          </sx:transform>
        </sx:when>
        <sx:otherwise>
          <sx:transform>
            <sx:content ref="countries"/>
          </sx:transform>
        </sx:otherwise>
      </sx:choose>
    </sx:serialize>
  </sx:service>

  <sx:recordContent id="countries" name="countries">
    <sx:recordStream>
      <sx:flatFileReader>
        <sx:urlSource url="data/countries.csv"/>
        <sx:flatFile ref="countriesFile"/>
      </sx:flatFileReader>
    </sx:recordStream>
    <sx:recordMapping ref="countriesToXmlMapping"/>
  </sx:recordContent>

  <sx:flatFile id="countriesFile">
    <sx:commentStarter value="#"/>
    <sx:flatFileHeader lineCount="1"/>
    <sx:flatFileBody>
      <sx:flatRecordType name="country">
        <sx:fieldDelimiter value=","/>
        <sx:delimitedField name="code"/>
        <sx:delimitedField name="name" trimLeading="true"/>
      </sx:flatRecordType>
    </sx:flatFileBody>
  </sx:flatFile>

  <sx:recordMapping id="countriesToXmlMapping">
    <countries>
      <sx:onRecord>
        <country>
          <sx:fieldElementMap field="name" element="countryName"/>
          <sx:fieldAttributeMap field="code" attribute="countryCode"/>
          <sx:fieldElementMap value="{$validate}" element="validate"/>
        </country>
      </sx:onRecord>
    </countries>
  </sx:recordMapping>

</sx:resources>

