Home > Enterprise >  XSLT: When more than one transformation is needed with XSLT 1.0?
XSLT: When more than one transformation is needed with XSLT 1.0?

Time:03-24

the XML file looks so:

<ROOT>
    <A>
        <B>
            <F name="Sandra"/>
            <F name="1234"/>
        </B>
        <C>
            <F name="Peter"/>
        </C>
    </A>
    <A>
        <B>
            <F name="Peter"/>
            <F name="nameles"/>
        </B>
        <C>
            <F name="1234"/>
        </C>
    </A>
</ROOT>

and that´s the xsl:

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

<xsl:output method="html"/>
<xsl:template match="/">
  <html>
  <body>    
        <table>
            <xsl:for-each select="//F">
                <tr>
                    <td>
                        <xsl:value-of select="@name"/>
                    </td>
                    <td>
                        <xsl:if test="ancestor::B">
                        <!-- doing something-->
                        </xsl:if>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

The first target is to have an HTML with a table which contains all names of the <F/>elements in one colum. In the second colum the parent node is checked and if the <F/>element is e.g. contained in the <B/> element a certain value is set. The extraction works fine and also getting getting the check of the parent node. I'm checking relative for each <F/>element in the complete file.

My problem is now that I get an unsorted table.

  1. Is it possible to store the hole table in a variable and sort in extra step with a second style sheet template?

Finally it is needed to see which <F/>elements with the same name exist, but are not containded in a <B/>element.

  1. Also for that the algorithm is clear, but is it possible to make step 1) and 2) in one XSLT transformation?

Or more general asked, when at least a second XSLT transformation must be done?

Regards

CodePudding user response:

Answering the headline question, you can always run a multi-phase transformation within a single stylesheet by using variables and modes --except that if you're using XSLT 1.0, you will need the exsl:node-set() extension to turn the result of the first phase, which you capture in a variable, into a node-set that can be processed in the second phase.

Whether it's better to do it one stylesheet or in multiple stylesheets chained together in a pipeline is another question. Using multiple stylesheets makes your code more modular and reusable, and easier to debug, but a bit trickier to deploy. A compromise is to do each phase of processing in a separate stylesheet module, and then use a top-level module (that includes/imports the others) to do the overall coordination.

  • Related