stm:apply-nsstm:apply-ns
STM by Example
Home > Books > Tutorials and Training Guides > STM Language > stm:apply-ns

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



Attribute
xpathrequired the target xpath into the operand document
prefixrequired prefix identifier to use for namespace uri
urirequired a uri for the namespace

A namespace is a URI associated with a document node which is expressed by a short-hand prefix on the element name. The stm:apply-ns operation applies a namespace to a document fragment located by an xpath reference to an element. The children of the target element are all moved into the namespace. The xmlns: declaration will be made on target element. If the target xpath is multivalued then each fragment will be processed and a namespace declaration will be made on each fragment root element.

Due to the historical difficulty of handling namespaces they have generally been infrequently used by XML applications. STM attempts to make assignment of namespaces straightforward. We anticipate that namespace assigment can be a valuable tool in XML processing. For example portions of an in-process document may be moved into a temporary namespace to hide it from intermediate processing steps before moving it back to the original namespace on completion.

stm:apply-ns is namespace aware, it cannot be used to overwrite an existing namespace. An existing namespace must first be removed before applying a new namepspace, see stm:remove-ns.

Limitations: The current implementation only handles namespace assignment to elements, though this should cover 90% of use cases.

Note: Remember that subsequent operations must use the namespace in any xpath that references these newly namespaced fragment!

Due to the limitations of the DOM model applying namespaces is a relatively expensive operation.

Example

Example 1: Apply a namespace

This example applies a namespace country to the first <b> element and it's children.

<idoc>
  <seq>
    <instr>
      <type>stm</type>
      <operand>doc.xml</operand>
      <operator>
        <stm:group xmlns:stm="http://1060.org/stm">
          <stm:apply-ns prefix="country" uri="http://1060.org/stm/apply-ns/eg1" xpath="/a/b[1]" />
        </stm:group>
      </operator>
      <target>this:response</target>
    </instr>
  </seq>
</idoc>

Try it

Example 2: Apply two different namespaces

This example demonstrates applying two different namespaces to each of the <b> elements in turn. Note both assignments are made to /a/b[1], this is not a mistake, the second assigment is to /a/b[1] since the initial /a/b[1] has become /a/country:b.

<idoc>
  <seq>
    <instr>
      <type>stm</type>
      <operand>doc.xml</operand>
      <operator>
        <stm:group xmlns:stm="http://1060.org/stm">
          <stm:apply-ns prefix="country" uri="http://1060.org/stm/apply-ns/eg1" xpath="/a/b[1]" />
          <stm:apply-ns prefix="seaside" uri="http://1060.org/stm/apply-ns/eg2" xpath="/a/b[1]" />
        </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.