Question

I am trying to parse this java comment:

/**
 * @parser
 *          path="/helloworld"
 *          href="/helloworld"
 *          
 *
 *
 *          path="/helloworld_2"
 *          href="/helloworld_2"
 *          
 *           
**/

I am using the following xsl code to parse it.

<span class="nowrap"> path: 
                <a>
                    <xsl:attribute name="href">
                        //code to output data
                    </xsl:attribute>

                    <xsl:value-of select="@path"/>
                </a>
            </span>

Currently I am getting an output, of just helloworld_2 with the code above.

if I only have the following in the comment

/**
 * @parser
 *          path="/helloworld"
 *          href="/helloworld"
 *          
**/

I will get the output of helloworld. How can I output both helloworld and helloworld_2 without having helloworld_2 overriding helloworld? Is there a way to loop around this code and have it still save the first helloworld and output it? I tried

<xsl:for-each select="@path">
<span class="nowrap"> path: 
                <a>
                    <xsl:attribute name="href">
                        //code to output data
                    </xsl:attribute>

                    <xsl:value-of select="@path"/>
                </a>
            </span>

             </xsl:for-each>

But that did not work.

Was it helpful?

Solution

You’re trying to parse non-XML content with XSL. Why?

<xsl:value-of select="@path"/> is intended to operate on something like this:

<element path="a path"/>

In that case, the value would be a path.

You can write XSL to parse these strings, but I recommend you find another way, such as parsing the string with Java and then writing XML with dom4j or JDOM.

If you’re really hung up on using XSL for this, here’s a simple stylesheet that gives an idea of how to proceed:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="root">
    <xsl:variable name="href" select="substring-before(substring-after(text(), 'href=&quot;'), '&quot;')" />
    <xsl:variable name="path" select="substring-before(substring-after(text(), 'path=&quot;'), '&quot;')" />
    <a href="{$href}"><xsl:value-of select="$path" /></a>
  </xsl:template>
</xsl:stylesheet>

Not too pleasant, as you can see.

When run on this document:

<root>
  /**
   * @parser
   *          path="/helloworld"
   *          href="/helloworld"
   *          
  **/
</root>

It produces this output:

<?xml version="1.0" encoding="UTF-8"?>
<a href="/helloworld">/helloworld</a>

If you’re serious about continuing, I’d recommend the more sophisticated technique of writing a named template that you can call repeatedly to parse out the information you want. Example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template name="parse-attribute">
    <xsl:param name="name" />
    <xsl:value-of select="substring-before(substring-after(text(), concat($name, '=&quot;')), '&quot;')" />
  </xsl:template>

  <xsl:template match="root">
    <xsl:variable name="href"><xsl:call-template name="parse-attribute"><xsl:with-param name="name" select="'href'" /></xsl:call-template></xsl:variable>
    <xsl:variable name="path"><xsl:call-template name="parse-attribute"><xsl:with-param name="name" select="'path'" /></xsl:call-template></xsl:variable>
    <a href="{$href}"><xsl:value-of select="$path" /></a>
  </xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top