[xslt] Intermittent problem.



Hi All,

I have created a generic stylesheet for transforming incoming requests
in to an internal format. For the most part, the transformation works
fine. At times, the transformation fails for requests that have worked
in the past. This results in an invalid XML document.

An example of failed transformation --

"Notice there is no root name"

<node name="QueryType" value="AccountNumber"/>
OrderQueryIMAU99CO19860808 <-- these are written to the console
<node name="PageLimit" value="200"/>
FalseFalseTrueNormal <-- these are written to the console
<node name="TimeOut" value="880"/>
FalseFalse1FMCF2EYSEZBFBMFFNRHBRHCRHGRHT

I am attaching the stylesheet. I am not sure whether this is a bug or
stylesheet error. Any help regarding this would be greatly appreciated.

I am using libxml2-2420, libxslt-1016

The high level stylesheet logic is -- For every node in the document,
    if it has only one text node, create a leaf node;
    otherwise create a parent node

Thanks,
Sai

The requests doc can be,

<Request>
<elem1>value1</elem1>
<elem2>value2</elem2>
<complexElem>
  <cElem1>cValue1</cElem1>
</complexElem>
</Request>

And the expected transformed doc is

<Request>
    <node name="elem1" value="value1"/>
    <node name="elem2" value="value2"/>
    <node name="complexElem" value=""/>
        <node name="cElem1" value="cValue1"/>
        <node name="cElem2" value="cValue2"/>
    </node>
</Request>
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:output method="xml" indent="yes"/>


  <!--Match children of root node, Request-->
  <xsl:template match="Request">
    <Request>
      <xsl:apply-templates/>
    </Request>
  </xsl:template>

  <!-- 
       Match on each child of the root node and convert the element
       node to the name attribute and the text node to the value 
       attribute
   -->

  <xsl:template match="*">

    <xsl:call-template name="createNode">
      <xsl:with-param name="aNode" select="."/>
    </xsl:call-template>

  </xsl:template>

  <!-- Procedure createNode -->
  <xsl:template name="createNode">

    <xsl:if test="count(child::text()) = 1">
      <xsl:call-template name="createLeafNode">
        <xsl:with-param name="aLeaf" select="."/>
      </xsl:call-template>
    </xsl:if>

    <xsl:if test="count(child::text()) != 1">
      <xsl:call-template name="createParentNode">
        <xsl:with-param name="aParent" select="."/>
      </xsl:call-template>
    </xsl:if>

  </xsl:template>


  <!-- Procedure createLeafNode -->
  <xsl:template name="createLeafNode">
    <node name="{name()}" value="{.}"/>
  </xsl:template>


  <!-- Procedure createParentNode -->
  <xsl:template name="createParentNode">
    <node name="{name()}" value="">
      <xsl:for-each select="./*">
        <xsl:call-template name="createNode">
          <xsl:with-param name="aNode" select="."/>
        </xsl:call-template>
      </xsl:for-each>
    </node>
  </xsl:template>


</xsl:stylesheet>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]