Re: [xsl] Confusing namespaces and xslt

Subject: Re: [xsl] Confusing namespaces and xslt
From: Evan Lenz <evan@xxxxxxxxxxxx>
Date: Tue, 30 Sep 2008 09:00:36 -0700
Make sure that we're using the same namespace URI. Suspecting an oversight, I added "http://"; to the beginning of the "client" namespace URI in the source XML. That was the only change to your XML that I made before testing the stylesheet below. It would also explain the behavior you're seeing. That rule will only match if the <work-managers> element is associated with the same exact namespace URI as the "client" prefix in the stylesheet. So changing the URI in either the stylesheet or the source document should fix it. Let me know if it doesn't!

Evan


Paul wrote:
Evan,

   Hmmm...the work-manager element isn't being added for some reason
for me using the snippet you provided. I like the "surgical" approach,
but I'm not sure why this snippet:

<!-- Append a <box:work-manager> element to the end of <work-managers>
content -->
<xsl:template mode="append" match="client:work-managers">
<box:work-manager>
<!-- etc. -->
</box:work-manager>
</xsl:template>

isn't running as expected. If I put in some debug statements the work-managers element is being handled by the transform (with the copy) later in your snippet, not but this focused snippet.


Paul



On Tue, Sep 30, 2008 at 10:28 AM, Paul <pflists@xxxxxxxxx> wrote:
Thanks! That is very helpful.

On Mon, Sep 29, 2008 at 5:57 PM, Evan Lenz <evan@xxxxxxxxxxxx> wrote:
I like to be as surgical as possible in my incremental transforms,
especially when I've got a lot of things to update. In this case, I might
use different modes, such as "content" and "append", as shown below:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:client="http://somecompany.com/box/client";
 xmlns:box="http://somecompany.com/box";
 exclude-result-prefixes="client">

<!-- BUSINESS LOGIC: -->
 <!-- Set the value for all <box:rotation-time> elements -->
 <xsl:template mode="content"
match="box:rotation-time">newvalue</xsl:template>

 <!-- Append a <box:work-manager> element to the end of <work-managers>
content -->
 <xsl:template mode="append" match="client:work-managers">
  <box:work-manager>
    <!-- etc. -->
  </box:work-manager>
 </xsl:template>


<!-- GENERIC UTILITY CODE: --> <!-- Identity transform for attributes, elements, comments, text, and PIs --> <xsl:template match="@* | node()"> <xsl:copy> <!-- Everything here (inside <xsl:copy>) only applies to elements --> <xsl:apply-templates select="@*"/> <xsl:apply-templates mode="content" select="."/> <xsl:apply-templates mode="append" select="."/> </xsl:copy> </xsl:template>

        <!-- By default, just process the existing content -->
        <xsl:template mode="content" match="*">
          <xsl:apply-templates/>
        </xsl:template>

        <!-- By default, don't append anything -->
        <xsl:template mode="append" match="*"/>

</xsl:stylesheet>

In other cases, I've also used "insert" (insert before existing content),
"before" (insert before the element itself), "after" (insert after the
element), and "att-value" (for updating just the attribute value, without
having to explicitly create a new attribute using <xsl:attribute> every
time).

Having such surgical precision can be very satisfying, but I don't usually
bother if there are only one or two things to update (as in this example).
But I thought I'd throw the idea out there anyway, in case you have lots of
things to update or you anticipate adding more update rules.

Evan

Current Thread