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

resolving template conflict?

Status
Not open for further replies.

mashadt

Instructor
Apr 23, 2005
33
ZA
in my XML I have a <chapter> root element. In this I have an <exercise> element and an <explanation> element. Each of these have child elements <heading> and <content>

I want my XSLT style sheet to process the heading and content elements the same regardless of where they are.

Ive done it by creating a template for //heading and //content.

But if I create anoter template for a parent of either of these, ( for exercise or explanation) then it overrides the earlier templates. How can I avoid this? I've tried assigning priorities but cant seem to get it right.


<xsl:template match="/">
<html>
<head>
<title>This is my first real try</title>
<link rel="stylesheet" href="try.css"/>

</head>
<body>

<h1>Chapter One</h1>
<xsl:apply-templates select="/chapter" />


</body>
</html>
</xsl:template>

<xsl:template match="//heading" >
<h2><xsl:apply-templates select="text()"/></h2>

</xsl:template>

<xsl:template match="//content" >

<xsl:apply-templates select="text()"/>
</xsl:template>

<!--<xsl:template match="explanation" >

<xsl:if test="solution">
<b>this is the solution</b>
</xsl:if>
<xsl:apply-templates select="text()"/>

</xsl:template>-->


<xsl:template match="instruction" >
<ul>
<li>

<xsl:if test="step">
<b>hello</b>
</xsl:if>
<xsl:apply-templates select="text()"/>
</li>
</ul>

</xsl:template>

</xsl:stylesheet>
 
How about this:
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>This is my first real try</title>
        <link rel="stylesheet" href="try.css"/>
      </head>
      <body>
        <h1>Chapter One</h1>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="heading">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="content">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="explanation">
    <xsl:if test="solution">
      <b>this is the solution</b>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="instruction">
    <ul>
      <li>
        <xsl:if test="step">
          <b>hello</b>
        </xsl:if>
        <xsl:apply-templates/>
      </li>
    </ul>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Ah HAH!

So I geuss

<xsl:apply-templates select="text()"/>

is redundant? I can just say

<xsl:apply-templates/>

unless I want to target some other node.

Thanks - learning this stuff by myself is very much a "one step forward three steps back" affair.
 
The default template is:
Code:
<xsl:template>
  <xsl:value-of select="."/>
</xsl:template>
So if you don't specify a template for a node, it will output the text.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
OK - still on the same topic. Now I'm trying to sort the elements. I would like to be able to have the <explanation> elements appear alphabetically according to the content of their <heading> child elements.

I've commented out my various attempts, each version of which gives me something I dont quite understand. The first try makes the last element (<instruction>) appear first? Why would that happen?

I think I understand why the second attempt makes everything else dissapear. There is a conflict in the templates there.

And the final one obviously does not work. So what is the answer here?

<xsl:template match="/">
<html>
<head>
<title>This is my first real try</title>
<link rel="stylesheet" href="try.css"/>

</head>
<body>
<h1>Chapter One</h1>

<xsl:apply-templates/>

</body>
</html>

</xsl:template>
<!-- This one makes the instruction element appear first

<xsl:template match="chapter" >
<xsl:apply-templates>
<xsl:sort select="heading" />
</xsl:apply-templates>

</xsl:template> -->

<!-- This one makes the instruction element dissapear

<xsl:template match="chapter" >
<xsl:apply-templates select="explanation">
<xsl:sort select="heading" />
</xsl:apply-templates>

</xsl:template>-->

<xsl:template match="heading">
<h2>
<xsl:apply-templates/>
</h2>
</xsl:template>

<xsl:template match="content">
<p><xsl:apply-templates/></p>
</xsl:template>

<!-- This one sorts the children of the explanation
element, and doesnt sort the explanation element by heading.

<xsl:template match="explanation">
<xsl:apply-templates>
<xsl:sort />
</xsl:apply-templates>

</xsl:template>-->


<xsl:template match="explanation">
This is the explanation template
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="instruction">
<ul>
<li>
<xsl:if test="step">
<b>hello</b>
</xsl:if>
<xsl:apply-templates/>
</li>
</ul>
</xsl:template>

</xsl:stylesheet>

Cant remember if I've changed the XML. Its
<chapter>
<explanation listing="2">
<heading>Explanation heading</heading>
<content>aFirst ExplanationCONTENT</content>
</explanation>
<explanation listing="1">
<heading>aSecond Explanation heading</heading>
<content>Second Explanations Content</content>
<solution>aExplanation Solution</solution>
</explanation>
<exercise>
<heading>Exercise heading</heading>
<content>Exercise Content</content>
</exercise>
<instruction>
<step>steps</step>instruction1</instruction>
<instruction>instruction2</instruction>
</chapter>
 
Instuction appears first because you're sorting all the nodes under chapter by heading. It doesn't find a value for heading for instruction, so I guess the sort algorithm puts that first.

Try this:
Code:
  <xsl:template match="/">
    <html>
      <head>
        <title>This is my first real try</title>
        <link rel="stylesheet" href="try.css"/>
      </head>
      <body>
        <h1>Chapter One</h1>
        <xsl:apply-templates select="chapter/explanation">
          <xsl:sort select="heading"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="chapter/exercise"/>
        <xsl:apply-templates select="chapter/instruction"/>
      </body>
    </html>
  </xsl:template>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top