1

I'm trying to transform an xhtml input to javascript output, my input is as follows:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>My page</title>
</head>

<body>
  <ul>
    <li><a href="page.html">about page</a></li>

    <li><a href="ipsum.html">ipsum</a></li>

    <li>there is a text right there

      <ul>

        <li><a href="dolor.html">dolor</a></li>

        <li><a href="yeah.html">yeah</a></li>

   </ul>
</ul>
</body>
</html>

And i want to produt an output file in a single line javascript to reuse for a menu, like this:

var mytree="";
mytree=' <ul><li><a href="page.html">about page</a></li><li><a href="ipsum.html">ipsum</a></li><li>there is a text right there<ul><li><a href="dolor.html">dolor</a></li> <li><a href="yeah.html">yeah/a></li></ul></ul>';

How can i produce this through xsl transformation ? I tried this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:html="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="html"
    version="2.0">
    <xsl:output  media-type="text/xml" method="xml" indent="no" omit-xml-declaration="yes"  ></xsl:output>
    <xsl:strip-space  elements="html:*"/> 
    <xsl:template match="html:html">
    <xsl:text disable-output-escaping="yes">toctree='</xsl:text>
 <xsl:apply-templates select="html:body"/>
    <xsl:text disable-output-escaping="yes">';</xsl:text>        
    </xsl:template>    
    <xsl:template name="copy" match="html:ul| html:li | html:a"  >
        <xsl:element name="{local-name()}"><xsl:for-each select="@*">
                <xsl:attribute name="{local-name()}">
                    <xsl:value-of select="replace(., '\n', '')" />
                </xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template> 
</xsl:stylesheet>

It works for me but there is always many line breaks ... and i need a single line to define variable mytree.

Thanks

1 Answer 1

1

Just add this template to the provided transformation:

<xsl:template match="text()">
 <xsl:value-of select="normalize-space()"/>
</xsl:template>

The complete transformation now becomes:

<xsl:stylesheet version="2.0" id="" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:html="http://www.w3.org/1999/xhtml" exclude-result-prefixes="html">
    <xsl:output  media-type="text/xml" method="xml" indent="no" omit-xml-declaration="yes"/>
    <xsl:strip-space  elements="html:*"/>

    <xsl:template match="html:html">
      <xsl:text disable-output-escaping="yes">toctree='</xsl:text>
      <xsl:apply-templates select="html:body"/>
      <xsl:text disable-output-escaping="yes">';</xsl:text>
    </xsl:template>

    <xsl:template name="copy" match="html:ul| html:li | html:a"  >
        <xsl:element name="{local-name()}">
           <xsl:for-each select="@*">
                <xsl:attribute name="{local-name()}">
                    <xsl:value-of select="replace(., '\n', '')" />
                </xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="text()">
     <xsl:value-of select="normalize-space()"/>
    </xsl:template>
</xsl:stylesheet>

and when applied on the provided XML document:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>My page</title>
</head>

<body>
  <ul>
    <li><a href="page.html">about page</a></li>

    <li><a href="ipsum.html">ipsum</a></li>

    <li>there is a text right there

      <ul>

        <li><a href="dolor.html">dolor</a></li>

        <li><a href="yeah.html">yeah</a></li>
      </ul>
    </li>
  </ul>
</body>
</html>

The wanted, one-line result is produced:

toctree='<ul><li><a href="page.html">about page</a></li><li><a href="ipsum.html">ipsum</a></li><li>there is a text right there<ul><li><a href="dolor.html">dolor</a></li><li><a href="yeah.html">yeah</a></li></ul></li></ul>';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.