0

I have an xml like this

 <text>
                <p>asd</p>

                <p>sdd</p>

                <p style="text-decoration: underline;">asa</p>

                <p>asd</p>

                <p>asad</p>
 </text>

And my xslt is:-

<xsl:template name="f1" match="*[local-name()='p']">
    <xsl:variable name="v1" select="@style"></xsl:variable>
    <xsl:choose>
      <xsl:when test="$v1 != contains($v1,'text-decoration: underline;')">
        <xsl:variable name="tagName" select="name()" />
        <xsl:element name="{$tagName}" namespace="{$nSpace}">
          <xsl:copy-of select="@*" />
          <xsl:apply-templates />
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$v1 = contains($v1,'text-decoration: underline;')">
          <xsl:element name="u" namespace="{$nSpace}">
            <xsl:apply-templates/>
          </xsl:element>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

My xslt is generating the output in this way:-

<text>
            <u>asd</u>

            <u>sdd</u>

            <u>asa</u>

            <u>asd</u>

            <u>asad</u>
</text>

I need output in this way

<text>
                <p>asd</p>

                <p>sdd</p>

                <p><u>asa</u></p>

                <p>asd</p>

                <p>asad</p>
    </text>

Can anyone help me to get this output. What changes are required in my xslt? Suggestions Welcome.

1 Answer 1

3

This is indeed a generic XML/XSLT question and not specific to Tridion. But here's how I would write it (having not seen the rest of your XSLT and ignoring the $nSpace part):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>

    <xsl:template match="p[contains(@style, 'text-decoration: underline')]">
        <p><u><xsl:value-of select="."/></u></p>
    </xsl:template>

    <!-- By default, copy all text nodes, elements and attributes -->   
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

The first template matches the one you care about: paragraphs with an underline style. The second template just copies everything else. That is a good starting point to transform XML where you just need to make tweaks: copy everything by default and then handle the exceptions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.