0

I am new in XSLT.I have an XML file and I want to use xslt to show the information in the xml file into a table. but I can the information in a row like this:

apfel 8.97 Fa. Krause Kirschen 10.45 Fa. Helbig apfel 12.67 Fa. Liebig this is my XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="/First.xsl"?>
<lieferungen xmlns="urn:myspace:lieferungen"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:myspace:lieferungen ....">
  <artikel id="3526">
    <name>apfel</name>
    <preis stueckpreis="true">8.97</preis>
    <lieferant>Fa. Krause</lieferant>
  </artikel>
  <artikel id="7866">
    <name>Kirschen</name>
    <preis stueckpreis="false">10.45</preis>
    <lieferant>Fa. Helbig</lieferant>
  </artikel>
</lieferungen>

and here is my XSLT:

<?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:template match="/">
    <html>
      <h1>The First XSLT in diesem Jahr</h1>
      <table>
        <tr>
          <td>Name</td>
          <td>Artikel</td>
          <td>Preis</td>
          <td>Liferant</td>
        </tr>
        <xsl:for-each select="artikel">
          <tr>
            <td>
              <xsl:value-of select="name"/>
            </td>
            <td>
              <xsl:value-of select="preis"/>
            </td>
            <td>
              <xsl:value-of select="lieferant"/>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </html>
  </xsl:template>
</xsl:stylesheet>

2 Answers 2

2

I offer another solution (also if I think that hr_117 offered a better one solution). Anyway I hope this may help you.

XML

<?xml version="1.0"?>
<lieferungen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:myspace:lieferungen ....">
   <artikel id="3526">
       <name>apfel</name>
       <preis stueckpreis="true">8.97</preis>
       <lieferant>Fa. Krause</lieferant>
   </artikel>
   <artikel id="7866">
        <name>Kirschen</name>
        <preis stueckpreis="false">10.45</preis>
        <lieferant>Fa. Helbig</lieferant>
   </artikel>
</lieferungen>

In the XML I removed the default namespace. And now the XSL:

<?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:template match="/">
<html>
  <h1>The First XSLT in diesem Jahr</h1>
  <table border="1">
    <tr>
      <td>Name</td>
      <td>Artikel</td>
      <td>Preis</td>
      <td>Liferant</td>
    </tr>
    <xsl:for-each select="//lieferungen/artikel">
        <tr><xsl:apply-templates select="name"/>
            <td><xsl:value-of select="@id"/></td>
            <xsl:apply-templates select="preis|lieferant"/>
        </tr>
    </xsl:for-each>
  </table>
</html>
</xsl:template>

 <xsl:template match="name">
   <td><xsl:value-of select="node()"/></td>
 </xsl:template>

 <xsl:template match="preis">
    <td><xsl:value-of select="node()"/></td>
 </xsl:template>

 <xsl:template match="lieferant">
    <td><xsl:value-of select="node()"/></td>
 </xsl:template>

</xsl:stylesheet>

This generatre the following html:

<html>
    <h1>The First XSLT in diesem Jahr</h1>
    <table border="1">
       <tr>
           <td>Name</td>
           <td>Artikel</td>
           <td>Preis</td>
           <td>Liferant</td>
      </tr>
      <tr>
           <td>apfel</td>
           <td>3526</td>
           <td>8.97</td>
           <td>Fa. Krause</td>
      </tr>
      <tr>
           <td>Kirschen</td> 
           <td>7866</td>
           <td>10.45</td>
           <td>Fa. Helbig</td>
      </tr>
   </table>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

thnks buddy, very nice :)
1

There are only small problems in your xslt. The main one is, that your XML has an default namespace. Therefore you need to add this namespace with an prefix to your stylesheet. Something like: xmlns:my="urn:myspace:lieferungen"

Than you have to use the new prefix my for any access to an element form default namespace. E.g.:

<xsl:for-each select="my:artikel">

Second: Your for-each loop iterate over artikel therefor you need to be in liefrant element to make this work. Change your template to <xsl:template match="/*">

Therefor try this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:my="urn:myspace:lieferungen" >
    <xsl:template match="/*">
        <html>
            <h1>The First XSLT in diesem Jahr</h1>
            <table>
                <tr>
                    <td>Name</td>
                    <td>Artikel</td>
                    <td>Preis</td>
                    <td>Liferant</td>
                </tr>
                <xsl:for-each select="my:artikel">
                    <tr>
                        <td>
                            <xsl:value-of select="my:name"/>
                        </td>
                        <td>
                            <xsl:value-of select="my:preis"/>
                        </td>
                        <td>
                            <xsl:value-of select="my:lieferant"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </html>
    </xsl:template>
</xsl:stylesheet>

Which will generate the following output:

<html xmlns:my="urn:myspace:lieferungen">
  <h1>The First XSLT in diesem Jahr</h1>
  <table>
    <tr>
      <td>Name</td>
      <td>Artikel</td>
      <td>Preis</td>
      <td>Liferant</td>
    </tr>
    <tr>
      <td>apfel</td>
      <td>8.97</td>
      <td>Fa. Krause</td>
    </tr>
    <tr>
      <td>Kirschen</td>
      <td>10.45</td>
      <td>Fa. Helbig</td>
    </tr>
  </table>
</html>

1 Comment

thnk you for your answer, I found somehow my problem, Th eproblem was the path of the Firs.xsl file in the xml file. If I put the absoulute path. It works, but now I have another problem. I can only show the HTML table and not the information in the xml file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.