<?xml version="1.0"?>

<sx:resources xmlns:sx="http://www.servingxml.com/core"
              xmlns:myns="http://mycompany.com/mynames/">

  <sx:service id="book-listing">
    <sx:recordStream>
      <sx:subtreeRecordReader>
        <sx:inverseRecordMapping ref="booksToFileMapping"/>
        <sx:transform>
          <sx:document/>
        </sx:transform>
      </sx:subtreeRecordReader>

      <!-- Convert category code to label -->
      <sx:choose>
        <sx:when test="category='F'">
          <sx:modifyRecord>
            <sx:newField name="category" value="Fiction"/>
          </sx:modifyRecord>
        </sx:when>
        <sx:when test="category='C'">
          <sx:modifyRecord>
            <sx:newField name="category" value="Computers"/>
          </sx:modifyRecord>
        </sx:when>
        <sx:when test="category='B'">
          <sx:modifyRecord>
            <sx:newField name="category" value="Biography"/>
          </sx:modifyRecord>
        </sx:when>
      </sx:choose>

      <sx:flatFileWriter>
        <sx:flatFile ref="booksFile"/>
      </sx:flatFileWriter>
    </sx:recordStream>
  </sx:service>

  <sx:flatFile id="booksFile">
    <sx:flatFileBody>
      <sx:flatRecordType ref="bookType"/>
    </sx:flatFileBody>
  </sx:flatFile>

  <sx:flatRecordTypeChoice id="bookType">
    <sx:delimitedField name="xxx"/>  <!-- unused here, used for input only -->
    <sx:when test="/book">  <!-- matches against record type "book" -->
      <sx:flatRecordType name="book">
        <sx:fieldDelimiter value=", "/>
        <sx:delimitedField name="title" quote="always"/>
        <sx:delimitedField name="author"/>
      </sx:flatRecordType>
    </sx:when>
    <sx:when test="/bookCategory">  <!-- matches against record type "bookCategory" -->
      <sx:flatRecordType name="book">
        <sx:fieldDelimiter value=" "/>
        <sx:delimitedField name="label"/>
        <sx:delimitedField name="category"/>
      </sx:flatRecordType>
    </sx:when>
    <sx:when test="/bookPrice">  <!-- matches against record type "bookPrice" -->
      <sx:flatRecordType name="book">
        <sx:fieldDelimiter value=" "/>
        <sx:delimitedField name="label"/>
        <sx:delimitedField name="price"/>
      </sx:flatRecordType>
    </sx:when>
  </sx:flatRecordTypeChoice>

  <sx:inverseRecordMapping id="booksToFileMapping">
    <sx:onSubtree path="/myns:books/myns:book">
      <sx:flattenSubtree recordType="book">
        <sx:subtreeFieldMap select="myns:title" field="title"/>
        <sx:subtreeFieldMap select="myns:author" field="author"/>
      </sx:flattenSubtree>
      <sx:flattenSubtree recordType="bookCategory">
        <sx:subtreeFieldMap select="'Category'" field="label"/>
        <sx:subtreeFieldMap select="@categoryCode" field="category"/>
      </sx:flattenSubtree>
      <sx:flattenSubtree recordType="bookPrice">
        <sx:subtreeFieldMap select="'Price'" field="label"/>
        <!-- Append dollar sign to price -->
        <sx:subtreeFieldMap select="concat('$',myns:price)" field="price"/>
      </sx:flattenSubtree>
    </sx:onSubtree>
  </sx:inverseRecordMapping>

</sx:resources>

