Menu

Dynamic XSL

2003-05-06
2003-05-08
  • Jacob Briscoe

    Jacob Briscoe - 2003-05-06

    What would be a good method for creating dynamic XSL?  I have tried to embed PHP into the XSL file but, was unsucessfull.  Is there is a way around this?

    Thank you,
    Jacob

     
    • rudi

      rudi - 2003-05-08

      Jacob,

      Here's some php/xsl code that may help.
      I'm learning all this stuff too and I hope this helps.
      I think the key is in the xsl:param tag.
      You can use this tag to enable 'dynamic' variables.

      Regards
      Rudi.

      Code:

      TRANSFORM XML WITH PHP AND XSL STYLESHEETS

      PHP is the weapon of choice for many Web warriors. Because of its
      intuitive language, robust functionality, cross-platform
      compatibility, and
      free availability, it's seen very frequently in both small shops and
      large
      enterprises.

      One feature that's often overlooked in PHP is its ability to parse XML
      documents with XSL stylesheets. Let's look at some of the basics of
      setting up an XSL parser in PHP to help you understand how you can
      put this
      feature to use.

      THE SAMPLES

      LISTING A shows a sample order document. We'll use this document as
      the
      input to our XSL process. The XSL stylesheet in LISTING B will also be
      used as input to our XSL process.

      LISTING A: order.xml

      <?xml version="1.0" ?>
      <Order>
        <Account>9900234</Account>
        <Item id="1">
          <SKU>1234</SKU>
          <PricePer>5.95</PricePer>
          <Quantity>100</Quantity>
          <Subtotal>595.00</Subtotal>
          <Description>Super Widget Clamp</Description>
        </Item>
        <Item id="2">
          <SKU>6234</SKU>
          <PricePer>22.00</PricePer>
          <Quantity>10</Quantity>
          <Subtotal>220.00</Subtotal>
          <Description>Mighty Foobar Flange</Description>
        </Item>
        <Item id="3">
          <SKU>9982</SKU>
          <PricePer>2.50</PricePer>
          <Quantity>1000</Quantity>
          <Subtotal>2500.00</Subtotal>
          <Description>Deluxe Doohickie</Description>
        </Item>
        <Item id="4">
          <SKU>3256</SKU>
          <PricePer>389.00</PricePer>
          <Quantity>1</Quantity>
          <Subtotal>389.00</Subtotal>
          <Description>Muckalucket Bucket</Description>
        </Item>
        <NumberItems>1111</NumberItems>
        <Total>3704.00</Total>
        <OrderDate>07/07/2002</OrderDate>
        <OrderNumber>8876</OrderNumber>
      </Order>

      LISTING B: order.xsl

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

        <xsl:param name="column" select="'SKU'"/>
      <xsl:param name="order"
      select="'ascending'"/>
        <xsl:template match="/">
          <html>
            <body>
              <xsl:apply-templates select="Order">
            <xsl:with-param name="sortcolumn"
      select="$column" />
            <xsl:with-param name="sortorder"
      select="$order" />
              </xsl:apply-templates>
            </body>
          </html>
        </xsl:template>

        <xsl:template match="Order">
          <xsl:param name="sortcolumn" />
          <xsl:param name="sortorder" />
          <table border="1">
            <tr>
              <th>Account</th>
              <th>SKU</th>
              <th>Description</th>
              <th>Price</th>
              <th>Quantity</th>
              <th>Subtotal</th>
            </tr>
            <xsl:apply-templates select="Item">
          <xsl:sort select="*[name()=$sortcolumn]"
      order="{$sortorder}"
      />
            </xsl:apply-templates>
          </table>
        </xsl:template>

        <xsl:template match="Item">
          <tr>
            <td><xsl:value-of select="../Account"
      /></td>
            <td><xsl:value-of select="SKU"
      /></td>
            <td><xsl:value-of select="Description"
      /></td>
            <td><xsl:value-of select="PricePer"
      /></td>
            <td><xsl:value-of select="Quantity"
      /></td>
            <td><xsl:value-of select="Subtotal"
      /></td>
          </tr>
        </xsl:template>   
      </xsl:stylesheet>

      OVERVIEW

      The gist of this example revolves around three XSL functions in PHP.
      We're going to start with creating an instance of the XSL engine.
      Next we'll
      send all of our input to the engine for processing and retrieve the
      results. Finally, we'll destroy the XSL engine, since we won't need
      it any
      longer.

      CREATING, PROCESSING, AND DESTROYING

      We're going to create a new XSL processor in memory. Rather than
      giving
      us an object, PHP is going to give us a handle we can use to interact
      with the other XSL functions. The command to create a new engine is
      xslt_create. The function returns the handle value, like this:

      $handle = xslt_create();

      In order to actually parse an XML document and process it with XSLT,
      you'll need to use PHP's xslt_process function. This function can
      take a
      variety of different parameters.

      In the most basic approach, which we'll use here, xslt_process takes
      three parameters. The first parameter is the handle to the XSL engine
      we
      created earlier. The second identifies a filename containing the
      input XML
      document, and the third identifies a filename containing the input XSL
      file. The function returns the result of the translation process.
      Here's a
      short example:

      $return = xslt_process($handle, $xmlfile, $xslfile);

      The final function is xslt_free. This function is used to destroy the
      instance of the engine that's in memory and free any associated
      resources.
      It takes a single parameter that points to the handle associated with
      the
      engine that's in memory, as shown here:

      xslt_free($handle);

      PUTTING IT TOGETHER

      Let's put together a short example that demonstrates PHP's ability to
      process XML documents with XSL stylesheets. We'll use the order shown
      in
      LISTING A as our input document and the stylesheet shown in LISTING B
      as
      our input XSL. LISTING C shows our complete PHP code for this
      demonstration.

      LISTING C: order.php

      <?php
      $xmlfile = "order.xml";
      $xslfile = "order.xsl";
      $args = array("column"=>"Quantity",
      "order"=>"descending");
      $engine = xslt_create();
      $output = xslt_process($engine, $xmlfile, $xslfile, NULL, NULL,
      $args);
      print $output;
      xslt_free($engine);
      ?>

      Notice that we've added a slight twist. In our XSL stylesheet, we can
      change the field we sort on, as well as the direction, by specifying
      some
      parameters. In this case, we have specified that the items in our
      order
      should be sorted by quantity in descending order. We used a PHP array
      to
      store the name-value pairs for our parameters, and then passed them
      to the
      engine via the xslt_process function.

       

Log in to post a comment.