XML technologies / XSLT / XSLT Cookbook / Query XML / Run structure-preserving queries

(Excerpt from “XSLT Kochbuch” by Sal Mangano)

Problem

You need to query an XML document so the answer has the same structure as the original.

Solution

Structure-preserving queries filter out irrelevant information while retaining most of the document structure. How closely the output resembles the input determines this recipe’s applicability: the closer the resemblance, the better it applies.

The recipe has two components, one reusable and one custom. The reusable component copies all nodes to the output, an identity transformation. We used this stylesheet, shown below, extensively under Exploiting the potential of XSLT 2.0.

Example: copy.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

The custom component imports copy.xslt and defines rules overriding its default behavior. For example, this stylesheet outputs the same structure as people.xml but includes only female smokers:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <!-- Leerräume zusammenfassen, die durch das Entfernen von person-Elementen entstehen -->
  <xsl:strip-space elements="people"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="person[@sex = 'w' and @smoker='ja']">
    <!-- Anwenden des vorgegebenen Verhaltens, d.h. des Kopierens -->
    <xsl:apply-imports/>
  </xsl:template>
  <!-- Andere Leute ignorieren -->
  <xsl:template match="person"/>
</xsl:stylesheet>

Alternatively, a single template can match what you want to exclude and do nothing with it:

<xsl:template match="person[@sex != 'w' or @smoker != 'ja']" />

Discussion

This recipe is very useful because it preserves an XML document’s structure without requiring you to know that structure. You only need to know which elements to filter out and create templates for them.

It also applies in contexts not usually described as queries. Suppose you want to clone an XML document while removing every attribute named sex and replacing them with an attribute named gender:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="@sex">
    <xsl:attribute name="gender">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

This works on any XML document regardless of its schema. If an element has an attribute named sex, it becomes gender:

Can you guess what this variant does? It outputs both attribute gender and attribute sex, but you knew that already!

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="copy.xslt"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="@sex">
    <xsl:attribute name="gender">
      <xsl:value-of select="."/>
    </xsl:attribute>
    <xsl:apply-imports/>
  </xsl:template>
</xsl:stylesheet>

  

 

 

 

back to top