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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with XPath Axes in XSL 1

Status
Not open for further replies.

sibl

Programmer
Apr 23, 2005
4
AT
Hello all

I need your help wiht XPath Axes in XSL for any XML. So that the XSL is valid against any XML.

I tried with ancestor-or-self::* and stuff like that but I think I dont really understand what it is all about.

For example I want to visualize following XML with an XSL that is valid for any XML:

<adressbuch>
<eintrag nr="1">
<name>Bernd Pack</name>
<adresse>Clausengasse 12</adresse>
<ort>1090 Wien</ort>
<email>bp@gmx.at</email>
<telefon>0650</telefon>
<geburtstag>12.12.1982</geburtstag>
</eintrag>
<eintrag nr="2">
<name>Patricia Bauer</name>
<adresse>Pflanzsteig 16</adresse>
<ort>1090 Wien</ort>
<email>pb@aon.at</email>
<telefon>06602</telefon>
<geburtstag>01.03.1957</geburtstag>
</eintrag>
</adressbuch>

So now I need a XSL which visualizes my XML above (and any other XML). For example every "eintrag" should be visualized with number1.jpg and any child -> "name" til "geburtstag" shall be visualized with number2.jpg. How shall i do that?

I know how to do it, when I know the tag-name but I dont know them in that case because it shall be valid for any XML.

So I tried the following XSL which does not really work:

<xsl:template match="/">
<xsl:element name="root">
<xsl:call-template name="visual"/>
</xsl:element>
</xsl:template>
<xsl:template name="visual">
<xsl:for-each select="//*">
<xsl:sort select="ancestor-or-self::*" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="ancestor-or-self::*"/>
</xsl:if>
</xsl:for-each>
</xsl:template>

I know I need to paste some choose and whens in I think to choose which jpg should be selected but my main problem is that the XPathes dont work, or better I dont understand them. Could somebody please help me???

Thanks a lot
sibl
 
So you're saying given a structure like:
Code:
<root>
  <node1>
    <node2>one</node2>
    <node2>two></node2>
  </node1>
</root>
You want something like:
Code:
<html>
  <head>
    <title>Visual XML</title>
  </head>
  <body>
    <img src="number1.jpg" alt="XML, node depth 1" />
    <img src="number2.jpg" alt="XML, node depth 2" />
    <img src="number2.jpg" alt="XML, node depth 2" />
  </body>
</html>
In which case you could use recursion:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>Visual XML</title>
      </head>
      <body>
        <xsl:for-each select="*/*">
          <xsl:call-template name="visualize">
            <xsl:with-param name="node" select="."/>
            <xsl:with-param name="depth" select="1"/>
          </xsl:call-template>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="visualize">
    <xsl:param name="node"/>
    <xsl:param name="depth"/>
    <img src="number{$depth}.jpg" alt="XML, node depth {$depth}"/>
    <xsl:if test="$node/*">
      <xsl:for-each select="$node/*">
        <xsl:call-template name="visualize">
          <xsl:with-param name="node" select="."/>
          <xsl:with-param name="depth" select="$depth+1"/>
        </xsl:call-template>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Thank you so much Jon.

This was exactly what I was looking for. That helped a lot.

Now there is another question you may be able to answer.. What if I want to make a table all around the input of the xml?

For example to have every depth in a new row with all its data in it?

<table border="0">
<tr>
<td>image1.jpg</td>
</tr>
<tr>
<td>image2.jpg</td>
<td>image2.jpg</td>
</tr>
<tr>
<td>image3.jpg</td>
etc....

How should that work? Because I can just do one row with all depth and data in it by building a table around <xsl:call-template>.

Hope u can help me again, if not no problem, you helped me a lot.

s
 
Me again.

I forgot to ask something else. I also want to give the values inside the tags - here one, two. It works but it also gives me the values of <node2>s at <node1> out and i dont want that.

It should be like this:

picture node1, no value
pictur node2, value node2
picture node2, value node2 etc..do you know what I mean?

thank you a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top