Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Why might an XSLT fail?</h2> <p>An XSLT will fail because of obvious things like typos. However, the most likely situation relates to namespace usage. If you declared a default namespace for your XML but don't include that in your XSLT, the XSLT won't match the templates as you might expect.</p> <p>The following example adds the <code>xmlns:business</code> attribute which declares that items qualified by the <code>business</code> prefix belong to the namespace <code>mynamespace.uri</code>. I then used this prefix to qualify the Address and Addresses template matches. Of course, you will need to change the namespace URI to whatever matches the default namespace of your XML file.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:business="mynamespace.uri" exclude-result-prefixes="msxsl"&gt; &lt;xsl:template match="/"&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*|node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="business:Addresses"&gt; &lt;xsl:element name="BusinessAddresses"&gt; &lt;xsl:apply-templates select="@*|node()" /&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;xsl:template match="business:Address"&gt; &lt;xsl:element name="BusinessAddress"&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <h2>How do you match templates based on element location as well as name?</h2> <p>There are several ways to achieve this last part to your problem, BusinessAddress or ContactAddress, but the easiest is to modify the template <code>match</code> attributes to consider parent nodes. If you think of the <code>match</code> attribute as a path into the XML for a node, this option becomes clearer (contents of templates left out for brevity):</p> <pre><code>&lt;xsl:template match="business:Business/business:Addresses&gt; &lt;/xsl:template&gt; &lt;xsl:template match="business:Business/business:Addresses/business:Address"&gt; &lt;/xsl:template&gt; &lt;xsl:template match="business:Contact/business:Addresses"&gt; &lt;/xsl:template&gt; &lt;xsl:template match="business:Contact/business:Addresses/business:Address"&gt; &lt;/xsl:template&gt; </code></pre> <p>Other methods exist for achieving this if the <code>match</code> remains based on just the element name, but they're harder to implement, follow, and maintain as they involve the use of conditional checks on the parent node hierarchy of the element being processing, all within the template.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload