Adding br tag in XSLT

Hi,

I want to add <br/> tag in xslt n below code.
How do we do that?
<xsl:for-each select="Features">
          <xsl:element name="Name">
            <xsl:value-of select="." disable-output-escaping="yes" />
            </br>
          </xsl:element>


        </xsl:for-each>

Open in new window


Please advise.

Regards
Dinesh BaliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
I don't understand your question. There is no </br> in HTML.
0
Dinesh BaliAuthor Commented:
</br> tag is giving me XSLT error.
Please advise.

<xsl:value-of select="." disable-output-escaping="yes" />
            </br>
 </xsl:element>
0
ste5anSenior DeveloperCommented:
Sorry, why do you want such a tag, not that it seems important that there is no such tag in HTML?

btw, a complete and concise example as well as a description of your use-case and some context would be help us to help you.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Dinesh BaliAuthor Commented:
Basically I need <br> after text <xsl:value-of select="." disable-output-escaping="yes" />

Please advise.
0
ste5anSenior DeveloperCommented:
Well then: enter it there.

In the previous posts you used </br>, which contained a slash.
0
Dinesh BaliAuthor Commented:
It is giving error. Not working
0
slightwv (䄆 Netminder) Commented:
I think you mean:   <br />

<xsl:for-each select="Features">
          <xsl:element name="Name">
            <xsl:value-of select="." disable-output-escaping="yes" />
            <br />
          </xsl:element>


        </xsl:for-each>

Open in new window



https://www.w3schools.com/tags/tag_br.asp
In XHTML, the <br> tag must be properly closed, like this: <br />.
0
Dinesh BaliAuthor Commented:
Yes <br />

When placing <br /> after <xsl:value-of select="." disable-output-escaping="yes" />
then XSLT is giving error.
0
slightwv (䄆 Netminder) Commented:
Sorry, I didn't take a hard look at the code.  I just copied/pasted.

I believe xsl:element can only have one value.  You need to move the break outside of that tag.
<xsl:for-each select="Features">
          <xsl:element name="Name">
            <xsl:value-of select="." disable-output-escaping="yes" />
          </xsl:element>
            <br />


        </xsl:for-each>

Open in new window

0
Dinesh BaliAuthor Commented:
I have multiple values in xslt with Name.

<xsl:element name="Name">

Therefore I need break within <xsl:element name="Name">
0
slightwv (䄆 Netminder) Commented:
Can you post sample data and expected results?
0
Dinesh BaliAuthor Commented:
XML:
<Product>
  <Features>
    <Name>Feature 1</Name>
    <Name>Feature 2</Name>
    <Name>Feature 3</Name>
  </Features>
  <Description>
    <![CDATA[
        <h4>Heading</h4>
					<p class="justify-align" >Summary Here.</p>]]>
  </Description>
  <Services>
    <![CDATA[
        <h4 >Key Services</h4>
					<h5 class="meeting-point" >Meeting point:
<br />
						<strong>meeting point 1</strong><a href="#">see map</a></h5>
            ]]>
  </Services>
</Product>

Open in new window


XSLT
<xsl:template match="Product">
    <tr>
      <td valign="top">
        <xsl:for-each select="Features">
          <xsl:element name="Name">
            <xsl:value-of select="." disable-output-escaping="yes" />
           <br />
          </xsl:element>
        </xsl:for-each>
      </td>
    </tr>
  </xsl:template>

Open in new window


Expected Result
<tr>
  <td valign="top">
    Feature 1  <br />
    Feature 2  <br />
    Feature 3
  </td>
</tr>

Open in new window

0
slightwv (䄆 Netminder) Commented:
Try this:
<xsl:template match="Features">
          <xsl:element name="Name">
              <xsl:for-each select="Name">
            <xsl:value-of select="." disable-output-escaping="yes" />
            <br />
            </xsl:for-each>
         </xsl:element>
</xsl:template>

<xsl:template match="Product">
    <tr>
      <td valign="top">
          <xsl:apply-templates/>
      </td>
    </tr>
  </xsl:template>

Open in new window


https://xsltfiddle.liberty-development.net/nc4NzQT/1
0
Geert BormansInformation ArchitectCommented:
I believe xsl:element can only have one value.

That is not correct, you can put anything inside an xsl:element element, as long as the remaining xslt is still wellformed XML
0
slightwv (䄆 Netminder) Commented:
>>That is not correct,

Yes, I remembered after I posted that.
0
Geert BormansInformation ArchitectCommented:
Here is what you can do
- split the templates to get rid of the for-each (that is a XSLT development best practice you should always do)
- get rid of the disable-output-escaping, the support for that is not mandatory, it breaks some processing and is highly recommended not to use

this works for me

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
    
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
        
    <xsl:template match="Product">
        <tr>
            <td valign="top">
                <xsl:apply-templates select="Features"/>
            </td>
        </tr>
    </xsl:template>
    
    <xsl:template match="Features">
        <xsl:element name="Name">
            <xsl:apply-templates select="Name"/>
         </xsl:element>
    </xsl:template>
    
    
    <xsl:template match="Name">
        <xsl:value-of select="."/>
        <br />
    </xsl:template>
    
    
    
</xsl:stylesheet>

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert BormansInformation ArchitectCommented:
I don't understand your question. There is no </br> in HTML.
give Dinesh a break please, I think it was fairly obvious what he wanted,

it is very common to put a <br /> in a html result if you need a line break.

Other than the small typo in the code to fix, I think the important message in this question is that an XSLT needs to be a wellformed XML document. Even if the end result after serialisation is HTML (and the serialiser will serialize the <br /> into <br> because that is what the browser expects)... you can only achieve that by having the wellformedness in the XSLT in order for the parser to actually not break on the XSLT itself

Having said that, netminder nailed this very early on in the question in his first comment... that is where the points should go

If the code I just posted is still giving errors (it is an innocent XSLT1) you should tell us how you run it
0
slightwv (䄆 Netminder) Commented:
>>that is where the points should go

Honestly, I prefer your template based approach.  I've used for-each in the past but prefer templates in all my new stuff.  After seeing yours, I'm disappointed I didn't complete it when I pulled Features out in my last example...  :(
0
Geert BormansInformation ArchitectCommented:
well look at it this way.... you started of in the right direction to show the good example and then you left the finalization of that exercise to the original poster...  ;-)
I just spoiled the party by finalizing that for him, my bad
1
slightwv (䄆 Netminder) Commented:
lol.... it isn't spoiled!  It's the correct way to solve the problem.  The upside is I dusted off my XSL that I haven't used in a while.  It was fun!
0
Dinesh BaliAuthor Commented:
Many Thanks for giving time and helping solve the problem.
0
Geert BormansInformation ArchitectCommented:
Dinesh we are more than happy to help.

Just want to give you a little hint for the future.
It is possible to accept more than one answer as the solution (by means of assisted solutions)
I guess nor netminder, nor myself care about the points.
But labeling multiple comments as solutions, shows your appreciation for the help you received from all that helped.
Just a thought
0
Dinesh BaliAuthor Commented:
Many Thanks Geert for help and suggestion.
I will take care.

Hi Guys,
Apologies from my side.

Regards,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.