1

Doing some work with xsl - first time I've done anything serious, and I've hit something which I can't explain. Easiest way to show it is with the identity transform:

This works:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

This doesn't (says "Unable to apply transformation on current source"):

<xsl:template match="@*|node()" xml:space='preserve'>
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

This does:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="node()" xml:space='preserve'/>
  </xsl:copy>
</xsl:template>

OK, I can see what's happening. But I don't understand why. Why does xml:space not want to play nicely with attributes? Just curious.

BTW, this is using the xsl translator that's built into Notepad++. Perhaps I shouldn't trust it?

Jules May
  • 753
  • 2
  • 10
  • 18
  • The first two look identical. What's the difference? – LarsH Apr 22 '13 at 18:28
  • Quite right! Finger trouble, sorry. Fixed now. – Jules May Apr 22 '13 at 18:37
  • OK, I see your fix... but it still seems like maybe your question is confused. You say "this does" work in regard to the example that uses `xml:space` applied to attributes (#3). But you say "xml:space" does not want to play nicely with attributes. Maybe you mislabeled your examples? – LarsH Apr 22 '13 at 18:39
  • Also, what is the "current source"? If you're using a buffer (or file) in Notepad++, is it possible that that file is not well-formed XML? – LarsH Apr 22 '13 at 18:41
  • Sorry, got that the wrong way round too! Too many test files spread out! Wouldn't expect that to work anyway, because the attributes precede the nodes. Fixing now... Not well-formed: That's a fair question. I think the source is well-formed (checked it, and it runs OK through the first and the third cases). Can I post it in a comment? – Jules May Apr 22 '13 at 18:47
  • Well... I still don't understand why you say "why does xml:space not want to play nicely *with attributes*." The example that throws an error has `xml:space` on an `xsl:template` element; xml:space is not interacting with attributes any more in that example than in others. Remember, `xml:space` applies to *the document in which it occurs*, namely, the **XSLT stylesheet**, not the input XML. (In fact xml:space should have no effect on anything, so if I were you I would just remove it.) – LarsH Apr 22 '13 at 18:59
  • > I still don't understand why you say "why does xml:space not want to play nicely with attributes." Because the failure happens when the space extends over the attribute selector. When (in the third example) it applies only to the node selector, everything works normally. – Jules May Apr 22 '13 at 19:14
  • OK, but since none of the `` elements have any whitespace-only text node descendants, it's probably not the `select="@*"` that makes a difference. Try moving `xml:space` from the `` to the ``; I suspect you still won't get an error. Even better, try a more robust XSLT processor like Saxon and see what errors you get. – LarsH Apr 22 '13 at 19:21
  • > Try moving xml:space from the to the ; That's exactly what I did to track down the problem. It fails. I think you're right about trying a different processor. From what you're saying, it's clearly not intended behaviour. – Jules May Apr 22 '13 at 19:32

2 Answers2

3

What are you trying to accomplish? xml:space="preserve" tells XML-consuming applications that you want to preserve whitespace-only text nodes that are descendants of the element that xml:space is an attribute of. In this example, you have xml:space as an attribute of <xsl:apply-templates>, but <xsl:apply-templates> has no whitespace-only text node descendants, so xml:space has no possible effect.

I think you wanted to preserve whitespace-only text nodes from the input XML document (not from the XSLT stylesheet). In that case, you need xml:space to be in the input XML document, not in the XSLT stylesheet. The stylesheet can have xsl:preserve-space-elements="*", but that's already the default, unless you have xsl:strip-space-elements set.

Yes, I would be inclined to wonder whether the XSLT processor used by Notepad++ (libxml) is doing something illegit. As a good diagnostic, try a respected processor like Saxon and see if you get any errors.

Either that, or just remove xml:space from your stylesheet, since it won't do you any good even if the processor doesn't throw an error.

Suggestion:

Just use

<xsl:output method="html" indent="yes"/>

as the first child of <xsl:stylesheet>. The indent="yes" will prevent all the output elements from being crammed together on one line, so you can read the results.

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • I'm just trying to avoid all the tags being crammed together on one line, so I can read the results! When it works, it has the desired effect: it correctly preserves the whitespace in the input document as it gets transformed to the output. Don't really care about preserving or not preserving whitespace in the attributes - they're all civilised strings anyway. It's just, I'm surprised that the transformer would refuse to proceed through the attributes when the space='preserve'is on - I'd have expected it just to ignore the setting. – Jules May Apr 22 '13 at 18:40
  • @Jules: `xml:space` on XSLT elements should not affect output XML unless there are descendants that are literal result elements. If you want whitespace in the input document preserved in the output doc, this happens by default (http://www.w3.org/TR/xslt#strip). – LarsH Apr 22 '13 at 18:46
  • I think I understand that. I discovered the problem inside another rule (below) that does have literals inside it. So, while I agree that it's no help in this example, the identity transform is the simplest thing I could find that showed the problem. I don't understand why it fails. `
    (Sorry - can;t get it to format)
    – Jules May Apr 22 '13 at 19:06
  • @Jules: see suggestion about using `indent="yes"`. – LarsH Apr 22 '13 at 20:40
  • Did that too :-) Doesn't seem to have any effect (but I am outputting xml rather than html). I'd found [this](http://stackoverflow.com/questions/2402212/how-do-i-make-xsl-transformation-indent-the-output), but I've not had a chance to explore it yet. (Been a long day) – Jules May Apr 22 '13 at 21:05
  • @Jules: regardless of html vs. xml, output method has no effect if the XSLT processor doesn't have control over serialization... which might be the case in Notepad++/libxml, I don't know. I'll be interested to hear how it goes with Saxon. – LarsH Apr 23 '13 at 13:22
  • Update: Not tried yet with Saxon (though I'm going to). But I note that when I do the translation in a browser, these failing cases fail in Google Chrome (Version 26.0.1410.64 m - blank screen, no error messages), but it works on an old version of Opera (Version 9.26 - though it rejects document ()) and on IE 8. Do you happen to know which xslt engine Chrome uses? – Jules May Apr 23 '13 at 20:39
  • @Jules: according to http://www.webkit.org/projects/xslt/, WebKit uses libxslt. Like Notepad++ does. So that's not much help. – LarsH Apr 24 '13 at 14:34
  • Actually, this is some help. I've filed a bug report [here](https://bugzilla.gnome.org/show_bug.cgi?id=698823). I'll let you know what they say. – Jules May Apr 25 '13 at 11:57
  • @Jules: ok. Your final statement on the bug report is interesting: "Naturally, to exhibit this bug, the code that's being transformed needs to have some attributes. If there's no attributes, there's no failure." It's bizarre that the error only occurs based on conditions in the input document (presence of attribute nodes) and conditions in the stylesheet (presence of xml:base="preserve"), which seem to have nothing to do with each other. – LarsH Apr 25 '13 at 13:52
  • Indeed: hence my continuing confusion :-) – Jules May Apr 25 '13 at 17:32
0

Whitespace is not preserved for attributes according to specification - it is highlighted in this posting. Preserving attribute whitespace in XSLT

Community
  • 1
  • 1
adelpreore
  • 61
  • 1
  • 5
  • I'm not complaining that it's not preserving the whitespace in the attributes. I'm complaining that the transformation is failing. Why is the space setting not just ignored inside the attributes? – Jules May Apr 22 '13 at 18:44