html - XSLT adding extra <br> tag when "copy-of" is used -


i transforming xml:

<?xml version='1.0' encoding='utf-8'?> <song xmlns="http://openlyrics.info/namespace/2009/song" version="0.8" createdin="openlp 2.0.1" modifiedin="openlp 2.0.1" modifieddate="2012-03-14t02:21:52">   <properties>     <titles>       <title>amazing grace</title>     </titles>     <authors>       <author>john newton</author>     </authors>   </properties>   <lyrics>     <verse name="v1">       <lines>amazing grace, how sweet sound<br/>that saved wretch me<br/>i once lost, found<br/>was blind see</lines>     </verse> 

with xslt:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:o="http://openlyrics.info/namespace/2009/song"> <xsl:output method="html" /> <xsl:template match="o:song"> <html> <body> <h2><xsl:value-of select="o:properties/o:titles/o:title"/><br /></h2> <h3><xsl:for-each select="o:properties/o:authors/o:author">   <xsl:value-of select="."/><br />   </xsl:for-each></h3>   <xsl:for-each select="o:lyrics/o:verse/o:lines">   <xsl:copy-of select="." />   </xsl:for-each> </body> </html> </xsl:template> 

each part of "lines" has br/ tag want keep, why i'm using copy-of instead of value-of. i'm trying "lines":

<lines xmlns="http://openlyrics.info/namespace/2009/song">amazing grace, how sweet sound<br/>that saved wretch me 

instead, xslt changes br/ tag into

<br></br> 

and yields 2 line breaks instead of 1 want.

<lines xmlns="http://openlyrics.info/namespace/2009/song">amazing grace, how sweet sound<br></br> 

is there way prevent br/ transformation , preserve br/ tags present in xml while still using copy-of? doing wrong here?

note: strangely, when delete namespace in "song" tag , write xslt if there no namespaces, such as

  <xsl:for-each select="lyrics/verse/lines">   <xsl:copy-of select="." /> 

then don't have problem. br/ tags preserved. feel has using namespace.

any ideas?

the problem not xslt, result document xslt serialised string.

you have specified output method of "html" in xslt, used serialiser, not output html. lines not html element, , furthermore, because has namespace, child br elements part of namespace, , these not seen html elements either.

when remove namespace, br seen html elements, , expected behavior, because serialisation process knows them.

one way around this, ensure br elements output without namespace. instead of using xsl:for-each lines, use xsl:apply-templates

<xsl:apply-templates select="o:lyrics/o:verse/o:lines" /> 

then have templates matching lines , br. in particular, template matching br output element without namespace.

try xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:o="http://openlyrics.info/namespace/2009/song" exclude-result-prefixes="o">    <xsl:output method="html"/>     <xsl:template match="o:song">       <html>          <body>             <h2>                <xsl:value-of select="o:properties/o:titles/o:title"/>                <br/>             </h2>             <h3>                <xsl:for-each select="o:properties/o:authors/o:author">                   <xsl:value-of select="."/>                   <br/>                </xsl:for-each>             </h3>             <xsl:apply-templates select="o:lyrics/o:verse/o:lines"/>          </body>       </html>    </xsl:template>     <xsl:template match="o:lines">       <xsl:copy>          <xsl:apply-templates/>       </xsl:copy>    </xsl:template>     <xsl:template match="o:br">       <br/>    </xsl:template> </xsl:stylesheet> 

this should output following

<html> <body> <h2>amazing grace<br></h2> <h3>john newton<br></h3> <lines xmlns="http://openlyrics.info/namespace/2009/song">    amazing grace, how sweet sound<br xmlns="">    saved wretch me<br xmlns="">    once lost, found<br xmlns="">    blind see </lines> </body> </html> 

this should @ least give single line-breaks when viewed in browser.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -