<?xml version="1.0"?>

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

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

      <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:flatFileHeader>
      <sx:flatRecordType ref="bookType"/>
    </sx:flatFileHeader>
    <sx:flatFileBody>
      <sx:flatRecordType ref="bookType"/>
    </sx:flatFileBody>
  </sx:flatFile>

  <sx:flatRecordType id="bookType" name="bookType">
    <sx:fieldDelimiter value="|"/>
    <sx:delimitedField name="category" label="Category"/>
    <sx:delimitedField name="author" label="Author"/>
    <sx:delimitedField name="title" label="Title"/>
    <sx:delimitedField name="price" label="Price"/>
    <sx:delimitedField name="review" label="review">
      <sx:subfieldDelimiter value=";"/>
    </sx:delimitedField>
  </sx:flatRecordType>

  <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="@categoryCode" field="category"/>
        <sx:subtreeFieldMap select="myns:author" field="author"/>
        <sx:subtreeFieldMap select="myns:price" field="price"/>
        <sx:subtreeFieldMap match="myns:reviews/myns:review" select="text()" field="review"/>
      </sx:flattenSubtree>
    </sx:onSubtree>                              
  </sx:inverseRecordMapping>

</sx:resources>

