Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL transformation Error

Status
Not open for further replies.
Sep 5, 2006
37
US
Hi,

Have the following sample xml, xsl and the jave code. But getting the following error:

Code:
Compiler warnings:
  line 18: Illegal attribute 'expr'.
ERROR:  'Syntax error in ''.'
FATAL ERROR:  'Could not compile stylesheet'
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:824)
	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFact

Here is the sample xml:
Code:
<?xml version="1.0"?>
<tree>
  <entity id="e1">
    <description>Customers</description>
    <oncontextmenu></oncontextmenu>
    <image>images/book.gif</image>
    <imageOpen>images/bookOpen.gif</imageOpen>
    <contents>
      <entity id="e2">
        <description>Microsoft</description>
        <image>images/book.gif</image>
        <imageOpen>images/bookOpen.gif</imageOpen>
        <onClick>displayCustomer(12345)</onClick>
        <contents>
          <entity id="e3">
            <description>Orders</description>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <onClick></onClick>
            <contents/>
          </entity>
        </contents>
      </entity>
      <entity id="e4">
        <description>IBM</description>
        <image>images/book.gif</image>
        <imageOpen>images/bookOpen.gif</imageOpen>
        <onClick>displayCustomer(12346)</onClick>
        <contents>
          <entity id="e5">
            <description>Orders</description>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <onClick></onClick>
            <contents/>
          </entity>
        </contents>
      </entity>
      <entity id="e6">
        <description>Sun Microsystems</description>
        <image>images/book.gif</image>
        <imageOpen>images/bookOpen.gif</imageOpen>
        <onClick>displayCustomer(12347)</onClick>
        <contents>
          <entity id="e7">
            <description>Orders</description>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <onClick></onClick>
            <contents>
              <entity id="e8">
                <description>#12345</description>
                <image>images/paper.gif</image>
                <imageOpen>images/paper.gif</imageOpen>
                <onClick></onClick>
                <contents/>
              </entity>
              <entity id="e9">
                <description>#12346</description>
                <image>images/paper.gif</image>
                <imageOpen>images/paper.gif</imageOpen>
                <onClick></onClick>
                <contents/>
              </entity>
            </contents>
          </entity>
        </contents>
      </entity>
      <entity id="e10">
        <description>Oracle</description>
        <image>images/book.gif</image>
        <imageOpen>images/bookOpen.gif</imageOpen>
        <onClick>displayCustomer(12348)</onClick>
        <contents>
          <entity id="e11">
            <description>Orders</description>
            <image>images/book.gif</image>
            <imageOpen>images/bookOpen.gif</imageOpen>
            <onClick></onClick>
            <contents/>
          </entity>
        </contents>
      </entity>
    </contents>
  </entity>
  <entity id="e12">
    <description>Reports</description>
    <oncontextmenu></oncontextmenu>
    <image>images/book.gif</image>
    <imageOpen>images/bookOpen.gif</imageOpen>
    <contents>
      <entity id="e13">
        <description>Income</description>
        <oncontextmenu></oncontextmenu>
        <image>images/paper.gif</image>
        <imageOpen>images/paper.gif</imageOpen>
        <contents>
        </contents>
      </entity>
      <entity id="e14">
        <description>Expenses</description>
        <oncontextmenu></oncontextmenu>
        <image>images/paper.gif</image>
        <imageOpen>images/paper.gif</imageOpen>
        <contents>
        </contents>
      </entity>
    </contents>
  </entity>
</tree>

And here is the XSL:
Code:
<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">

<xsl:template match="tree">
  <xsl:apply-templates select="entity"/>
</xsl:template>

<xsl:template match="entity">
  <div onclick="window.event.cancelBubble = true;clickOnEntity(this);" onselectstart="return false" ondragstart="return false">
  <xsl:attribute name="image"><xsl:value-of select="image"/></xsl:attribute>
  <xsl:attribute name="imageOpen"><xsl:value-of select="imageOpen"/></xsl:attribute>
  <xsl:attribute name="open">false</xsl:attribute>
  <xsl:attribute name="id">f<xsl:value-of select="@id"/></xsl:attribute>
  <xsl:attribute name="open">false</xsl:attribute>
  <xsl:attribute name="STYLE">
    padding-left: 20px;
    cursor: hand;
    <xsl:if expr="depth(this) > 2">
      display: none;
    </xsl:if>
  </xsl:attribute>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="middle">
          <img border="0" id="image">
            <xsl:attribute name="SRC">
              <xsl:value-of select="image"/>
            </xsl:attribute>
          </img>
        </td>
        <td valign="middle" nowrap="true">
        <xsl:attribute name="STYLE">
          padding-left: 7px;
          font-family: Verdana;
          font-size: 11px;
          font-color: black;
        </xsl:attribute>
        <xsl:value-of select="description"/></td>
      </tr>
    </table>
  <xsl:apply-templates select="contents/entity"/>
  </div>
</xsl:template>

</xsl:stylesheet>

Here is the java code:
Code:
            XslTransform xslTransform = new XslTransform () ;
            TransformerFactory tFactory = TransformerFactory.newInstance () ;
            Transformer transformer = tFactory.newTransformer ( new StreamSource ( new File ( "C:\\tree.xsl" ) ) ) ;
            transformer.transform ( new StreamSource ( "C:\\tree.xml" ) , new StreamResult ( new FileOutputStream ( "C:\\treeout.txt" ) ) ) ;

The error is due to the following in xsl

Code:
<xsl:if expr="depth(this) > 2">
      display: none;
    </xsl:if>

Any idea on how to fix this error to get the right output. Any help is appreciated.

Thanks
 
I think there's a problem with the xsl, you should ask this in the XML forum.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top