Can anyone give me a solution to the following problem?

<root>
   <elem1>
      <titel><bold>1.<tab/>The text <italic>of the</italic> headline</bold></titel>
   </elem1>
   <par>...</par>
   <par/>
</root>

Should become the following structure (attention: double bold-element):

<root>
   <elem1>
      <titel>
         <nummer><bold>1.</bold></nummer>
         <text><bold>The text <italic>of the </italic> headline</bold></text>
      </titel>
   </elem1>
</root>

Thanks a lot!

Recommended Answers

All 3 Replies

Since nobody else has offered a solution, I will try and help you. Here is one way of generating lines 4 and 5 of the desired output.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"/>

<xsl:template match="/root">
   <xsl:apply-templates />
</xsl:template>

<xsl:template match="titel/bold">
    <nummer><bold><xsl:value-of select="normalize-space(text()[1])"/></bold></nummer><xsl:text>
    </xsl:text><text><bold><xsl:copy-of select="descendant::node()[position()>2]"/></bold></text>
</xsl:template>

</xsl:stylesheet>

The text element is there to provide a newline in the output. You should be able to handle the rest of the transformation.

Here is the output:

<?xml version="1.0"?>

    <nummer><bold>1.</bold></nummer>
    <text><bold>The text <italic>of the</italic>of the headline</bold></text>

Thanks fpmurphy,

the solution looks good, but only for exactly the same elements I had posted.

But my problem will be a little different:

what will happen if the element <bold> doesn't exist? What will happen if there's an element <italic> instead of <bold>?

<elem1>
<titel>1.<tab/>The text <italic>of the</italic> headline</titel>
</elem1>
...
<elem1>
<titel><italic>1.<tab/>The text <bold>of the</bold> headline</italic></titel>
</elem1>

Try the following stylesheet.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"/>

<xsl:template match="/root">
   <xsl:apply-templates />
</xsl:template>

<xsl:template match="titel">
    <xsl:choose>
       <xsl:when test="descendant::node()[1][self::*]">
          <xsl:variable name="fpm" select="name(descendant::node()[1])"/>
          <nummer><xsl:element name="{$fpm}">
          <xsl:value-of select="normalize-space(descendant::text()[1])"/>
          </xsl:element></nummer><xsl:text>
      </xsl:text>
          <xsl:variable name="nodeset" select="descendant::node()[position()>3]"/>
          <text><xsl:element name="{$fpm}">
          <xsl:copy-of select="$nodeset" />
          </xsl:element></text>
       </xsl:when>
       <xsl:otherwise>
          <nummer>
          <xsl:value-of select="normalize-space(text()[1])"/>
          </nummer>
          <xsl:text>
      </xsl:text>
          <xsl:variable name="nodeset" select="descendant::node()[position()>2]"/>
          <text>
          <xsl:copy-of select="$nodeset" />
          </text>
       </xsl:otherwise>
    </xsl:choose>

</xsl:template>

</xsl:stylesheet>

which transforms the following example document

<?xml version="1.0" encoding="ISO-8859-1"?>

<root>
   <elem1>
      <titel><italic>1.<tab/>The text <bold> of the </bold> headline </italic></titel>
      <titel>2.<tab/>The text <bold> of the </bold> headline</titel>
      <titel><bold>3.<tab/>The text <bold> of the </bold> headline </bold></titel>
   </elem1>
</root>

into

<nummer><italic>1.</italic></nummer>
      <text><italic>The text <bold> of the </bold> of the  headline </italic></text>
      <nummer>2.</nummer>
      <text>The text <bold> of the </bold> of the  headline</text>
      <nummer><bold>3.</bold></nummer>
      <text><bold>The text <bold> of the </bold> of the  headline </bold></text>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.