Dear Gerrit,
Thank you for the explanation! I understand now why I get the @xmlns
attribute with Dimitre's solution. But I still don't understand why I
also get it with Graydon's solution, which does not use <xsl:copy> but
serialize() followed by parse-xml-fragment(). In the input data, the
@xmlns attribute is not on the <i> element itself but on an ancestor
element several levels up. Why then does it show up in the result of
serialize()? And how do I get rid of it using Graydon's approach?
Best,
Wolfhart
On 15.08.20 01:57, Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx wrote:
Dear Wolfhart,
If the input elements are in a certain namespace, then seeing a
namespace *declaration* (xmlns:prefix="uri") is probably less of a
problem than seeing the original <i> element in whatever namespace it
used to be (<i xmlns="orig-ns">).
In this case, even <xsl:copy copy-namespaces="no"> wouldn't help
because if the <i> is reproduced by xsl:copy, it will keep its
namespace. Only declarations that are not needed will be left out, but
i's namespace *is* needed if it remains in its original namespace.
In order to "copy" the source elements, but put them in another
namespace, you don't use <xsl:copy>. You can use <xsl:element
name="{local-name()}"> in order to keep the (unprefixed, if the input
elements had a prefix) name but put the result in whatever namespace
the rest of the output is in.
hth
Gerrit
On 15.08.2020 07:25, Wolfhart Totschnig wolfhart.totschnig@xxxxxxxxxxx
wrote:
Thank you very much, Graydon and Dimitre, for your replies! Your
solutions work very well and deepened my understanding of XSLT.
There is just one little problem I encountered: The data of the
external source comes in a particular namespace. Since I do not want
to have the namespace declaration in the output, I use
@exclude-result-prefixes in the <xsl:stylesheet> element, which works
as expected for the rest of the output produced by my stylesheet and
also for the <title> and <subtitle> elements produced by your
solutions. However, on the <i> elements, the namespace declaration
suddenly shows up, which baffles me. Do you know why that is? And how
do I get rid of it?
Best,
Wolfhart
On 14.08.20 23:08, Dimitre Novatchev dnovatchev@xxxxxxxxx wrote:
XSLT 2.0:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
B <xsl:output omit-xml-declaration="yes" indent="yes"/>
B B <xsl:template match="node()|@*" mode="copy-lowercase">
B B <xsl:copy>
B B B <xsl:apply-templates select="node()|@*" mode="#current"/>
B B </xsl:copy>
B </xsl:template>
B <xsl:template match="title/text()[contains(., ':')]">
B B <title>
B B B <xsl:apply-templates select="preceding-sibling::node()"
mode="copy-lowercase"/>
B B B <xsl:copy-of select="lower-case(substring-before(., ':'))"/>
B B </title>
B B <subtitle>
B B B <xsl:copy-of select="lower-case(substring-after(., ':'))"/>
B B B <xsl:apply-templates select="following-sibling::node()"
mode="copy-lowercase"/>
B B </subtitle>
B </xsl:template>
B <xsl:template match="text()"/>
B <xsl:template match="title//text()"
mode="copy-lowercase"><xsl:value-of
select="lower-case(.)"/></xsl:template>
</xsl:stylesheet>
When applied on the provided XML document:
<title>THE TITLE OF THE BOOK WITH SOME
B B <i>ITALICS</i> AND SOME MORE
WORDS: THE SUBTITLE OF THE BOOK WITH SOME
B B <i>ITALICS</i>
</title>
the wanted result is produced:
<title>the title of the book with some
<i>italics</i> and some more
words</title>
<subtitle> the subtitle of the book with some
<i>italics</i>
</subtitle>
B --
Cheers,
Dimitre Novatchev
On Fri, Aug 14, 2020 at 7:16 PM Wolfhart Totschnig
wolfhart.totschnig@xxxxxxxxxxx
<mailto:wolfhart.totschnig@xxxxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx
<mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>> wrote:
B B B Dear list,
B B B I would like to ask for your help with the following mixed-content
B B B problem. I am receiving, from an external source, data in the
B B B following
B B B form:
B B B <title>THE TITLE OF THE BOOK WITH SOME <i>ITALICS</i> AND SOME MORE
B B B WORDS: THE SUBTITLE OF THE BOOK WITH SOME <i>ITALICS</i></title>
B B B What I would like to do is
B B B 1) separate the title from the subtitle (i.e., divide the data
at the
B B B colon) and put each in a separate element node;
B B B 2) all the while maintaining the <i> markup;
B B B 3) and perform certain string manipulations on all of the text
nodes;
B B B for the purposes of this post, I will use the example of converting
B B B upper-case to lower-case.
B B B So the desired output is the following:
B B B <title>the title of the book with some <i>italics</i> and some more
B B B words</title>
B B B <subtitle>the subtitle of the book with some
<i>italics</i></subtitle>
B B B How can this be done?
B B B I know that I can perform string manipulations while maintaining
B B B the <i>
B B B markup with templates, i.e., <xsl:template match="text()"/> and
B B B <xsl:template match="i"/>. But in this case I do not know how to
B B B divide
B B B the data at the colon. And I know that I can divide the data at the
B B B colon with <xsl:value-of select="substring-before(.,': ')"/>, but
B B B then I
B B B loose the <i> markup. So I am at a loss.
B B B Thanks in advance for your help!
B B B Wolfhart
|