Menu

root parameter use in stylesheet

Help
2003-09-05
2003-11-06
  • Alex Prilop

    Alex Prilop - 2003-09-05

    Well, I have the following problem:

    ( I already posted it in the support, but got no answer and I get desperate as I need to finish a task...)

    I ran into a foreach loop and need to access the whole xml tree in the stylesheet.
    I found the parameter $root and can even print it out (so I see that I have the total information), but how do I form valid XPathes with this parameter, as I cannot reach the XML ancestor nodes with the regular expressions: (../child or /ancestor::child or /desc/child .....) nor with the use of the root parameter
    Something as $root/child does not work neither /$root/child. ( I added a  <xsl:param name=root /> to the stylesheet and then I can access $root)

    Somehow I seem to miss the point, and misunderstood the meaning of the parameter root.

    Thanks in advance for the help

    Alex

     
    • Franck Wolff

      Franck Wolff - 2003-11-06

      Hi,

      Sorry, I wasn't monitoring this "Help" forum and maybe this is to late now...

      Anyway, this little (and silly) example could help you:

      persons.xml
      ---------------
      <persons>
        <people good="true">
          <name>John Smith</name>
        </people>
        <people good="false">
          <name>Bonnie Parker</name>
          <name>Clide Barrow</name>
        </people>
      </persons>

      generate.xml
      ---------------
      <project name="letters" default="letters">

        <taskdef name="ejen" classname="org.ejen.ant.Ejen"/>

        <target name="letters">
          <ejen stacktrace="true">

            <echo message="Reading persons.xml"/>
            <source uri="persons.xml"/>

            <foreach select="/table/people/name">
              <echo message="Saving {.}.txt"/>
              <template uri="letter.xsl" save="{.}.txt"/>
            </foreach>

          </ejen>
        </target>
      </project>

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

        <xsl:output method="text" encoding="iso-8859-1"/>
        <xsl:param name="root"/>

        <xsl:template match="name">
          1. <xsl:value-of select="../@good"/>
          2. <xsl:value-of select="$root//*[name=current()]/@good"/>
          3. <xsl:value-of select="$root//people[name=current()]/@good"/>
          4. <xsl:value-of select="$root/table/people[name=current()]/@good"/>
        </xsl:template>
      </xsl:stylesheet>

      1 gives no result (empty string). 2, 3 and 4 give the correct result ("true" for JS, "false" for BP and CB).

      Regards,

      Franck.

       

Log in to post a comment.