title: Mulesoft: Default value for not existing deep nodes
body: <p>I use Data Weave to transform XML and have issue with not existing nodes. For example xml</p>
  
  <pre><code>&lt;a&gt;
     &lt;b&gt;
        &lt;c&gt;
           value
        &lt;/c&gt;
     &lt;/b&gt;
  &lt;/a&gt; 
  </code></pre>
  
  <p>Extracted { one: $.a, two: $a.b, three: $a.b.c } It works fine when all nodes a,b and c exist.</p>
  
  <p>What would be the way to determine when node b is empty and have default value for node c?</p>
  
  <p>This is really simply example. Real data has a lot of deepness levels and each level could be empty. Maybe I should use when/otherwise but then I should repeat whole data structure for each variable.</p>
  
  <p><a href="https://i.stack.imgur.com/prPUo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/prPUo.png" alt="enter image description here"></a></p>
  
  <p>Here is Mule flow:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:spring="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
  http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
  http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
  http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd"&gt;
      &lt;flow name="xml2flatFlow" doc:description="To run copy src/main/resources/*.xml files to src/test/resources/FileIn
  Good working example is xml_in_good.xml
  
  Issues:
  
  1) Should be working with Streams but from Json2Flat there is no way to FileOut. &amp;quot;Load everything to memory&amp;quot; is required but it kills stream. What is the way to make it Stream and work with huge files?
  
  2) FlatFile field have particular length. Transformation should cut the length. Instead it produces exception. Should be option oe way to force the length of the string.
  Example: xml_in_bad_too_long.xml
  
  3) Main issue - I cannot figure out how process empty nodes. 
  Example: xml_in_bad.xml
  
  Provided example is simlpiest. In reality I have very long xpath like payload.a.b.c.d.e.f.g.h
  I tried to do when/otherwise, default, h?, internal Java program - nothing works.
  
  I believe that processor File2Json is not necessary but I'd added it to make sure XML processor have no influence on the flow. "&gt;
          &lt;file:inbound-endpoint path="src\test\resources\FileIn" moveToDirectory="src\test\resources\FileProcessed" responseTimeout="10000" doc:name="FileIn"/&gt;
          &lt;dw:transform-message metadata:id="24cbc998-ba7d-44f7-a3fa-cb502d352e87" doc:name="File2Json"&gt;
              &lt;dw:input-payload mimeType="application/xml"/&gt;
              &lt;dw:set-payload&gt;&lt;![CDATA[%dw 1.0
  %output application/json
  ---
  payload]]&gt;&lt;/dw:set-payload&gt;
          &lt;/dw:transform-message&gt;
          &lt;logger message="#[payload]" level="INFO" doc:name="Logger"/&gt;
          &lt;dw:transform-message metadata:id="15c2b229-fb5a-4430-be89-c446e79bfa2e" doc:name="Json2Json"&gt;
              &lt;dw:input-payload doc:sample="C:\Users\Alex\AnypointStudio6\workspace\xml2flat\src\main\resources\too_long.json" mimeType="application/json"/&gt;
              &lt;dw:set-payload&gt;&lt;![CDATA[%dw 1.0
  %output application/json
  ---
  {
      a: payload.a default {},
      b: payload.a.b default {},
      c: payload.a.b.c default ""
  }]]&gt;&lt;/dw:set-payload&gt;
          &lt;/dw:transform-message&gt;
          &lt;dw:transform-message metadata:id="8ef7c277-ec47-4564-9ece-eac11c27c891" doc:name="Json2Flat"&gt;
              &lt;dw:input-payload doc:sample="C:\Users\Alex\AnypointStudio6\workspace\xml2flat\src\main\resources\too_long.json"/&gt;
              &lt;dw:set-payload&gt;&lt;![CDATA[%dw 1.0
  %output text/plain schemaPath = "flatfile.ffd" , segmentIdent = ""
  ---
  [{
      field_0: payload.c default "---"
  }]]]&gt;&lt;/dw:set-payload&gt;
          &lt;/dw:transform-message&gt;
          &lt;object-to-string-transformer doc:name="Load everythin to memory"/&gt;
          &lt;file:outbound-endpoint path="src/test/resources/FileOut" outputPattern="out.txt" responseTimeout="10000" doc:name="FileOut"/&gt;
      &lt;/flow&gt;
  &lt;/mule&gt;
  </code></pre>
  
  <p>Here is log:</p>
  
  <pre><code>*******************************************************************************************************
  * xml2flatSO                                    * default                        * DEPLOYED           *
  *******************************************************************************************************
  
  INFO  2016-09-30 13:57:29,271 [[xml2flatSO].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Users\Alex\AnypointStudio6\workspace\xml2flatSO\src\test\resources\FileIn\xml_in_good.xml
  INFO  2016-09-30 13:57:37,892 [[xml2flatSO].xml2flatFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {
    "a": {
      "b": {
        "c": "GOOD"
      }
    }
  }
  INFO  2016-09-30 13:57:42,334 [[xml2flatSO].connector.file.mule.default.dispatcher.01] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'connector.file.mule.default.dispatcher.546298364'. Object is: FileMessageDispatcher
  INFO  2016-09-30 13:57:42,334 [[xml2flatSO].connector.file.mule.default.dispatcher.01] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'connector.file.mule.default.dispatcher.546298364'. Object is: FileMessageDispatcher
  INFO  2016-09-30 13:57:42,335 [[xml2flatSO].connector.file.mule.default.dispatcher.01] org.mule.transport.file.FileConnector: Writing file to: C:\Users\Alex\AnypointStudio6\workspace\xml2flatSO\src\test\resources\FileOut\out.txt
  INFO  2016-09-30 13:57:52,448 [[xml2flatSO].connector.file.mule.default.receiver.01] org.mule.transport.file.FileMessageReceiver: Lock obtained on file: C:\Users\Alex\AnypointStudio6\workspace\xml2flatSO\src\test\resources\FileIn\xml_in_bad.xml
  INFO  2016-09-30 13:57:55,979 [[xml2flatSO].xml2flatFlow.stage1.02] org.mule.api.processor.LoggerMessageProcessor: {
    "a": ""
  }
  ERROR 2016-09-30 13:58:16,403 [[xml2flatSO].xml2flatFlow.stage1.02] org.mule.exception.DefaultMessagingExceptionStrategy: 
  ********************************************************************************
  Message               : Exception while executing: 
      b: payload.a.b default {},
         ^
  Type mismatch for 'Value Selector' operator
       found :string, :name
    required :datetime, :name or
    required :localdatetime, :name or
    required :object, :name or
    required :time, :name or
    required :array, :name or
    required :date, :name or
    required :localtime, :name or
    required :period, :name
  Payload               : {
    "a": ""
  }
  Payload Type          : java.lang.String
  Element               : /xml2flatFlow/processors/3 @ xml2flatSO:xml2flat.xml:37 (Json2Json)
  Element XML           : &lt;dw:transform-message metadata:id="15c2b229-fb5a-4430-be89-c446e79bfa2e" doc:name="Json2Json"&gt;
                          &lt;dw:input-payload doc:sample="C:\Users\Alex\AnypointStudio6\workspace\xml2flat\src\main\resources\too_long.json" mimeType="application/json"&gt;&lt;/dw:input-payload&gt;
                          &lt;dw:set-payload&gt;%dw 1.0%output application/json---{a: payload.a default {},b: payload.a.b default {},c: payload.a.b.c default ""}&lt;/dw:set-payload&gt;
                          &lt;/dw:transform-message&gt;
  --------------------------------------------------------------------------------
  Root Exception stack trace:
  com.mulesoft.weave.engine.ast.dynamic.UnexpectedOperationTypesException: Type mismatch for 'Value Selector' operator
       found :string, :name
    required :datetime, :name or
    required :localdatetime, :name or
    required :object, :name or
    required :time, :name or
    required :array, :name or
    required :date, :name or
    required :localtime, :name or
    required :period, :name
      at com.mulesoft.weave.engine.ast.OpNode$class.doExecuteAutoCoercion(OpNode.scala:79)
      at com.mulesoft.weave.engine.ast.BinaryOpNode.doExecuteAutoCoercion(BinaryOpNode.scala:8)
      at com.mulesoft.weave.engine.ast.OpNode$class.doExecute(OpNode.scala:33)
      at com.mulesoft.weave.engine.ast.BinaryOpNode.doExecute(BinaryOpNode.scala:8)
      at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
      at com.mulesoft.weave.engine.ast.BinaryOpNode.execute(BinaryOpNode.scala:8)
      at com.mulesoft.weave.engine.ast.selectors.NullSafeNode.doExecute(NullSafeNode.scala:16)
      at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
      at com.mulesoft.weave.engine.ast.selectors.NullSafeNode.execute(NullSafeNode.scala:10)
      at com.mulesoft.weave.engine.ast.conditional.DefaultNode.doExecute(DefaultNode.scala:13)
      at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
      at com.mulesoft.weave.engine.ast.conditional.DefaultNode.execute(DefaultNode.scala:10)
      at com.mulesoft.weave.engine.ast.structure.KeyValuePairNode.doExecute(KeyValuePairNode.scala:12)
      at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
      at com.mulesoft.weave.engine.ast.structure.KeyValuePairNode.execute(KeyValuePairNode.scala:10)
      at com.mulesoft.weave.engine.ast.structure.ObjectNode$$anonfun$4.apply(DynamicObjectNode.scala:39)
      at com.mulesoft.weave.engine.ast.structure.ObjectNode$$anonfun$4.apply(DynamicObjectNode.scala:38)
      at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
      at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
      at scala.collection.Iterator$class.foreach(Iterator.scala:742)
      at scala.collection.AbstractIterator.foreach(Iterator.scala:1194)
      at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
      at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
      at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
      at scala.collection.AbstractTraversable.map(Traversable.scala:104)
      at com.mulesoft.weave.engine.ast.structure.ObjectNode.doExecute(DynamicObjectNode.scala:38)
      at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:38)
      at com.mulesoft.weave.engine.ast.structure.ObjectNode.execute(DynamicObjectNode.scala:28)
      at com.mulesoft.weave.engine.ast.ValueNode$class.write(AstNode.scala:28)
      at com.mulesoft.weave.engine.ast.structure.ObjectNode.write(DynamicObjectNode.scala:28)
      at com.mulesoft.weave.engine.Engine.write(Engine.scala:165)
      at com.mulesoft.weave.engine.Engine.execute(Engine.scala:88)
      at com.mulesoft.weave.engine.Engine.execute(Engine.scala:103)
      at com.mulesoft.weave.mule.debug.DebugWeaveExecutorStrategy$$anonfun$execute$1.apply(DebugWeaveExecutorStrategy.scala:30)
      at com.mulesoft.weave.mule.debug.DebugWeaveExecutorStrategy$$anonfun$execute$1.apply(DebugWeaveExecutorStrategy.scala:28)
      at scala.Option.map(Option.scala:146)
      at com.mulesoft.weave.mule.debug.DebugWeaveExecutorStrategy.execute(DebugWeaveExecutorStrategy.scala:28)
      at com.mulesoft.weave.mule.WeaveMessageProcessor.execute(WeaveMessageProcessor.scala:117)
      at com.mulesoft.weave.mule.WeaveMessageProcessor.process(WeaveMessageProcessor.scala:65)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
      at org.mule.execution.MessageProcessorNotificationExecutionInterceptor.execute(MessageProcessorNotificationExecutionInterceptor.java:108)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:88)
      at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
      at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:27)
      at org.mule.execution.MessageProcessorExecutionTemplate.execute(MessageProcessorExecutionTemplate.java:44)
      at org.mule.processor.BlockingProcessorExecutor.executeNext(BlockingProcessorExecutor.java:98)
      at org.mule.processor.BlockingProcessorExecutor.execute(BlockingProcessorExecutor.java:59)
      at org.mule.interceptor.AbstractEnvelopeInterceptor.processBlocking(AbstractEnvelopeInterceptor.java:58)
      at org.mule.processor.AbstractRequestResponseMessageProcessor.process(AbstractRequestResponseMessageProcessor.java:47)
      at org.mule.processor.AsyncInterceptingMessageProcessor.processNextTimed(AsyncInterceptingMessageProcessor.java:123)
      at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.process(AsyncInterceptingMessageProcessor.java:208)
      at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker$1.process(AsyncInterceptingMessageProcessor.java:201)
      at org.mule.execution.ExecuteCallbackInterceptor.execute(ExecuteCallbackInterceptor.java:16)
      at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:35)
      at org.mule.execution.CommitTransactionInterceptor.execute(CommitTransactionInterceptor.java:22)
      at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:30)
      at org.mule.execution.HandleExceptionInterceptor.execute(HandleExceptionInterceptor.java:14)
      at org.mule.execution.BeginAndResolveTransactionInterceptor.execute(BeginAndResolveTransactionInterceptor.java:67)
      at org.mule.execution.ResolvePreviousTransactionInterceptor.execute(ResolvePreviousTransactionInterceptor.java:44)
      at org.mule.execution.SuspendXaTransactionInterceptor.execute(SuspendXaTransactionInterceptor.java:50)
      at org.mule.execution.ValidateTransactionalStateInterceptor.execute(ValidateTransactionalStateInterceptor.java:40)
      at org.mule.execution.IsolateCurrentTransactionInterceptor.execute(IsolateCurrentTransactionInterceptor.java:41)
      at org.mule.execution.ExternalTransactionInterceptor.execute(ExternalTransactionInterceptor.java:48)
      at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:28)
      at org.mule.execution.RethrowExceptionInterceptor.execute(RethrowExceptionInterceptor.java:13)
      at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:110)
      at org.mule.execution.TransactionalErrorHandlingExecutionTemplate.execute(TransactionalErrorHandlingExecutionTemplate.java:30)
      at org.mule.processor.AsyncInterceptingMessageProcessor$AsyncMessageProcessorWorker.doRun(AsyncInterceptingMessageProcessor.java:200)
      at org.mule.work.AbstractMuleEventWork.run(AbstractMuleEventWork.java:53)
      at org.mule.work.WorkerContext.run(WorkerContext.java:286)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  
  ********************************************************************************
  </code></pre>
  
  <p>Any suggestion?</p>