xsl-list
[Top] [All Lists]

Re: [xsl] problem with transforming mixed content

2020-08-15 00:25:43
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(_at_)gmail(_dot_)com wrote:
XSLT 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="title/text()[contains(., ':')]">
    <title>
      <xsl:apply-templates select="preceding-sibling::node()" mode="copy-lowercase"/>
      <xsl:copy-of select="lower-case(substring-before(., ':'))"/>
    </title>
    <subtitle>
      <xsl:copy-of select="lower-case(substring-after(., ':'))"/>
      <xsl:apply-templates select="following-sibling::node()" mode="copy-lowercase"/>
    </subtitle>
  </xsl:template>

  <xsl:template match="text()"/>

  <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
   <i>ITALICS</i> AND SOME MORE
WORDS: THE SUBTITLE OF THE BOOK WITH SOME
   <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>

  --
Cheers,
Dimitre Novatchev


On Fri, Aug 14, 2020 at 7:16 PM Wolfhart Totschnig wolfhart(_dot_)totschnig(_at_)mail(_dot_)udp(_dot_)cl <mailto:wolfhart(_dot_)totschnig(_at_)mail(_dot_)udp(_dot_)cl> <xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com <mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:

    Dear list,

    I would like to ask for your help with the following mixed-content
    problem. I am receiving, from an external source, data in the
    following
    form:

    <title>THE TITLE OF THE BOOK WITH SOME <i>ITALICS</i> AND SOME MORE
    WORDS: THE SUBTITLE OF THE BOOK WITH SOME <i>ITALICS</i></title>

    What I would like to do is
    1) separate the title from the subtitle (i.e., divide the data at the
    colon) and put each in a separate element node;
    2) all the while maintaining the <i> markup;
    3) and perform certain string manipulations on all of the text nodes;
    for the purposes of this post, I will use the example of converting
    upper-case to lower-case.

    So the desired output is the following:

    <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>

    How can this be done?

    I know that I can perform string manipulations while maintaining
    the <i>
    markup with templates, i.e., <xsl:template match="text()"/> and
    <xsl:template match="i"/>. But in this case I do not know how to
    divide
    the data at the colon. And I know that I can divide the data at the
    colon with <xsl:value-of select="substring-before(.,': ')"/>, but
    then I
    loose the <i> markup. So I am at a loss.

    Thanks in advance for your help!
    Wolfhart



XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/2652055> (by email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>