Tree OperationsTree Operations
STM by Example
Home > Books > Tutorials and Training Guides > STM Language > Tree Operations

Rate this page:
Really useful
Satisfactory
Not helpful
Confusing
Incorrect
Unsure
Extra comments:


This section shows design patterns in which STM is used to perform tree processing. The stm operations are not described in detail in this section it is for illustration of some general uses of STM.

STM is often lighter-weight, easier to write and more efficient in performance than XSLT. It is also more convenient than custom code. That said, XSLT is best for recursive document transformations.

Example

Example 1: Tree Pruning.

Remove all elements with a price less than 100
This example shows how a single command can be used to prune a tree for subsequent processing.

<idoc>
  <seq>
    <instr>
      <type>stm</type>
      <operand>data.xml</operand>
      <operator>
        <stm:group xmlns:stm="http://1060.org/stm">
          <stm:delete xpath="/data/item[price &lt; 100]" />
        </stm:group>
      </operator>
      <target>this:response</target>
    </instr>
  </seq>
</idoc>

Try it

Example 2: Namespace Filtering.

Remove all elements with a price less than 100 and highlight all elements with a price greater than 500

This example shows a similar filter to the first but this time all filtered items are moved into the "expensive" namespace. Namespaces are very easy to manipulate using STM.

<idoc>
  <seq>
    <instr>
      <type>stm</type>
      <operand>data.xml</operand>
      <operator>
        <stm:group xmlns:stm="http://1060.org/stm">
          <stm:delete xpath="/data/item[price &lt; 100]" />
          <stm:apply-ns prefix="expensive" uri="temp:/what/a/rip/off" xpath="/data/item[price &gt; 500]" />
        </stm:group>
      </operator>
      <target>this:response</target>
    </instr>
  </seq>
</idoc>

Try it

Example 3: Tree Transformation.

As example 2 above but then rename highlighted item to luxury-item

Similar to before only this time we'll rename all "expensive:item" to "expensive:luxury-item". This shows how operations subsequent to a namespace declaration can incorporate the namespace into the target xpath to process a subsection of the original document.

<idoc>
  <seq>
    <instr>
      <type>stm</type>
      <operand>data.xml</operand>
      <operator>
        <stm:group xmlns:stm="http://1060.org/stm">
          <stm:delete xpath="/data/item[price &lt; 100]" />
          <stm:apply-ns prefix="expensive" uri="temp:/what/a/rip/off" xpath="/data/item[price &gt; 500]" />
          <stm:rename xpath="/data/expensive:item">luxury-item</stm:rename>
        </stm:group>
      </operator>
      <target>this:response</target>
    </instr>
  </seq>
</idoc>

Try it

© 2003-2007, 1060 Research Limited. 1060 registered trademark, NetKernel trademark of 1060 Research Limited.