Write a Transform

The transform specifies what to extract from the data.You can use any authoring environment and language appropriate for your project. For XML transformations, choose a technology such as XSLT, Joost (STX), Java, Python, or Perl, based on the goals and scope of the project.

In the price example, the next step is to transform the XML data into a simple two-column delimited format.

  1. 708421|19.99
  2. 708466|59.25
  3. 711121|24.99

The following STX transform, called input_transform.stx, completes the data transformation.

  1. <?xml version="1.0"?>
  2. <stx:transform version="1.0"
  3. xmlns:stx="http://stx.sourceforge.net/2002/ns"
  4. pass-through="none">
  5. <!-- declare variables -->
  6. <stx:variable name="itemnumber"/>
  7. <stx:variable name="price"/>
  8. <!-- match and output prices as columns delimited by | -->
  9. <stx:template match="/prices/pricerecord">
  10. <stx:process-children/>
  11. <stx:value-of select="$itemnumber"/>
  12. <stx:text>|</stx:text>
  13. <stx:value-of select="$price"/> <stx:text>
  14. </stx:text>
  15. </stx:template>
  16. <stx:template match="itemnumber">
  17. <stx:assign name="itemnumber" select="."/>
  18. </stx:template>
  19. <stx:template match="price">
  20. <stx:assign name="price" select="."/>
  21. </stx:template>
  22. </stx:transform>

This STX transform declares two temporary variables, itemnumber and price, and the following rules.

  1. When an element that satisfies the XPath expression /prices/pricerecord is found, examine the child elements and generate output that contains the value of the itemnumber variable, a | character, the value of the price variable, and a newline.
  2. When an <itemnumber> element is found, store the content of that element in the variable itemnumber.
  3. When a element is found, store the content of that element in the variable price.