Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm fighting with xpath... I try do do something like a 'SQL-JOIN', but without any success. I have an xml doc with master/detail relation 1:1. The xsl below shows me all masters, but accessing its detail does not work (third column). N.B: I'm an absolute beginner with xsl and xpath.

Thanks for help

Really, nobody an idea?

XML
<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:template match="/">
  <html>
  <body>

  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Master ID</th>
      <th>Master Name</th>
      <th>Detail</th>
    </tr>
    <xsl:for-each select="Export/Master">
    <tr>
      <td><xsl:value-of select="MASTER_ID"/></td>
      <td><xsl:value-of select="MASTER_NAME"/></td>
      <!--The following is the problem...-->   
      <td><xsl:value-of select="/Export/Detail/DETAIL_DATA[/Export/Detail/MASTER_ID = MASTER_ID]"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 


xml example data:

XML
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="dcical.XSL"?>
<Export>
  <Master>
    <MASTER_ID>ID1</MASTER_ID>
    <MASTER_NAME>Name 1</MASTER_NAME>
  </Master>
  <Master>
    <MASTER_ID>ID2</MASTER_ID>
    <MASTER_NAME>Name 2</MASTER_NAME>
  </Master>
  <Detail>
    <MASTER_ID>ID1</MASTER_ID>
    <DETAIL_DATA>Detail of of master 1</DETAIL_DATA>
  </Detail>
</Export>
<!-- Integrity= "57687a72" -->
Posted
Updated 28-May-12 12:58pm
v8
Comments
[no name] 28-May-12 17:06pm    
Really, nobody an idea? Or is maybe my question probably a little bit abstract ...to short... not clear?
Andreas Gieriet 28-May-12 18:08pm    
What is the data?
What is the problem? E.g. no data in the table or crap or XSLT error, or ...?
Make it easy for the rest of the world to reproduce your problem. Or shall I invent the data myself? Give all you have, destilled to the relevant parts and ask for advise.

Cheers
Andi
[no name] 28-May-12 18:43pm    
Danke, Du hast recht. ich werde gleich mal auch die Daten posten ;)
No erros, simply no data in the third column.

After you posted the data: this works for me.

XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="html" indent="yes"/>

  <xsl:template match="/Export">
    <html>
      <body>

        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Master ID</th>
            <th>Master Name</th>
            <th>Detail</th>
          </tr>
          <xsl:for-each select="Master">
            <xsl:variable name="mid" select="MASTER_ID"/>
            <tr>
              <td>
                <xsl:value-of select="MASTER_ID"/>
              </td>
              <td>
                <xsl:value-of select="MASTER_NAME"/>
              </td>
              <!--The following solves the problem...-->
              <td>
                <xsl:variable name="data"
                     select="../Detail[MASTER_ID=$mid]/DETAIL_DATA"/>
                <xsl:choose>
                  <xsl:when test="$data">
                      <xsl:value-of select="$data"/>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:text>&#160;</xsl:text>
                  </xsl:otherwise>
                </xsl:choose>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>


</xsl:stylesheet>



The resulting HTML is:

XML
<html>
  <body>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Master ID</th>
        <th>Master Name</th>
        <th>Detail</th>
      </tr>
      <tr>
        <td>ID1</td>
        <td>Name 1</td>
        <td>Detail of of master 1</td>
      </tr>
      <tr>
        <td>ID2</td>
        <td>Name 2</td>
        <td>&#160;</td>
      </tr>
    </table>
  </body>
</html>


Cheers
Andi
 
Share this answer
 
v2
Comments
[no name] 29-May-12 10:57am    
Great! Thanks a lot.
Maybe this is what you aim for:

XML
<xsl:for-each select="Export/Master">
  <xsl:variable name="masterid" select="MASTER_ID"/>
  ...
  <td>
     <xsl:value-of select="/Export/Detail[MASTER_ID=$masterid]/DETAIL_DATA"/>
  </td>
  ...


Cheers
Andi
 
Share this answer
 
Comments
[no name] 28-May-12 18:55pm    
sorry, does also not work...but keep in mind I'm the absolute beginner with this xpath stuff. maybe I try something which is not possible..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900