Re: [xsl] How to retain the end tag when outputting an empty element?

Subject: Re: [xsl] How to retain the end tag when outputting an empty element?
From: "David Carlisle d.p.carlisle@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 17 Jun 2022 18:47:11 -0000
If you just need specific elements serialised this way rather than change
them all you can  give the element content that serialises to nothing, eg

<xsl:stylesheet version="3.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";>
 <xsl:character-map name = "emptytag">
  <xsl:output-character character = "X" string = "" />
 </xsl:character-map>
 <xsl:output use-character-maps="emptytag"/>

 <xsl:template match="/">
 1 <aaa></aaa>
 2 <aaa>X</aaa>
 </xsl:template>

 </xsl:stylesheet>

which if run on itself produces

<?xml version="1.0" encoding="UTF-8"?>
 1 <aaa/>
 2 <aaa></aaa>

of course you can replace X by any character not otherwise used, such as a
private use or unallocated Unicode codepoint

David

On Fri, 17 Jun 2022 at 18:25, Bauman, Syd s.bauman@xxxxxxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> MK> <test></test> is the canonical form. I've no idea why. Perhaps
> because it's simpler, in the sense that all elements are serialized the
> same way regardless of whether or not they are empty. (With no need for
> special rules about edge cases, like elements that are empty except for
> comments).
>
> I do not know why, either, but I have always presumed exactly the reasons
> you state. Why? Because I would do it that way for those reasons.
>
>
> As for Rogerbs original question, I am fond of lying to my students,
> telling them that whether an XML processor writes b<duck/>b or
> b<duck></duck>b is up to the processor; since they are the same as far
as
> XML is concerned, you (the human) do not get to say which you want. (I
> typically confess this oversimplification later in the course, explaining
> that a processor does not *have* to give you a choice, but it is welcome
> to, and some do.)
>
> Using this (admittedly simplified) theory of XML processing, though, the
> answer to bhow to get <duck></duck> instead of <duck/>b is
> $ perl -pe 's,<([A-Za-z][A-Za-z0-9._-]+)/>,<$1></$1>,g;' < INPUT.xml >
> OUTPUT.xml
> or similar.
>
> (Where the regular expression I use here is one that will match all TEI
> element names without a namespace prefix, and obviously may need to be
> adjusted for other use cases.)
>
> Note, of course, that this non-XML solution changes b<!-- do not use
> "<blort/>", use "<blort></blort>" instead -->b into the hilariously
> difficult to figure out b<!-- do not use "<blort></blort>", use
> "<blort></blort>" instead -->b. p
>
>
> ------------------------------
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/2739265> (by
> email <>)

Current Thread