You need to query an XML document so that the response has a structure that is identical to the original.
Structure-preserving queries filter out irrelevant information while preserving most of the document structure. The degree by which the output structure resembles the structure of the input is the metric that determines the applicability of this example. The more similar it is, the more this example applies.
The example has two components—one reusable and the other custom. The reusable component is a stylesheet that copies all nodes to the output (identity transform). We used this stylesheet, shown in Example 7-9, extensively in Chapter 6.
Example 7-9. 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="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet>
The custom component is a stylesheet that imports
copy.xslt
and creates rules to override its
default behavior. For example, the following stylesheet results in
output identical to people.xml
, but with 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"/> <!-- Collapse space left by removing person elements --> <xsl:strip-space elements="people"/> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="person[@sex = 'f' and @smoker='yes']"> <!-- Apply default behavior, which is to copy --> <xsl:apply-imports/> </xsl:template> <!-- Ignore other people --> <xsl:template match="person"/> </xsl:stylesheet>
Alternatively, a single template can match the things that you want to exclude and do nothing with them:
<xsl:template match="person[@sex != 'f' or @smoker != 'yes']" />
This example is extremely useful because it lets you preserve the structure of an XML document without necessarily knowing what its structure is. You only need to know what elements should be filtered out and that you create templates that do so.
This example is applicable in contexts that most people would not
describe as queries. For example, suppose you wanted to clone an XML
document, but remove all attributes named sex
and
replace them with an attribute called 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>
The beauty of this example is that it works on any XML document,
regardless of its schema. If the document has elements with an
attribute named sex
, they will become
gender
:
Can you guess what the following variation does?[16]
<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>