tags:

views:

65

answers:

1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="xml" encoding="utf-8" />
  <xsl:template match="/">
    <html>
      <body>
        <span>
          <div style="background-color:#000066;color:#EEEEEE;padding:7px">
            <a name="top" style="padding-left:10px;font-size:28pt">Alerting Variables</a>
          </div>
        </span>
        <div style="display:block;padding-left:50px;padding-bottom:10px" class="hbuttons">
          <a href="CoreAlerts.xml">LDMS Alerts</a>
          <a href="ServerManagerAlerts.xml">LDSM Alerts</a>
          <a href="why.html">Why?</a>
          <a href="examples.html">Examples</a>
          <a href="resources.html">Resources</a>
        </div>
        <div style="clear: left;"></div>
        <!-- This is the Table of Contents-->
        <div style="padding:5px">
          <div style="padding:5px;margin-top:10pt;margin-bottom:10pt;font-weight:bold;font-size:20px">Table of Contents - 
          <a style="position:absolute;margin-left:40px" href="PrintPages/PrintAll.html">
            <img border="0" src="images/PrintButton.png" />
          </a></div>
          <div style="font-family:Arial;font-weight:bold;margin-left:30px;font-size:10pt">
            <xsl:if test="contains(identifiers/sectionname/alert/@name, 'Agent Watcher')">
              <a href="#AgentWatcher">Agent Watcher</a>
              <a style="position:absolute;margin-left:40px" href="PrintPages/PrintAW.html">
                <img border="0" src="images/PrintButton.png" />
              </a>
            </xsl:if>
            <ol style="margin-top:5">
              <xsl:for-each select="identifiers/sectionname/alert">
                <xsl:if test="contains(@name, 'Agent Watcher')">
                  <li style="margin-left:10pt;font-size:8pt">
                    <a>
                      <xsl:attribute name="href">#
                      <xsl:value-of select="@name" /></xsl:attribute>
                      <xsl:value-of select="@name" />
                    </a>
                  </li>
                </xsl:if>
              </xsl:for-each>
            </ol>
          </div>
          <div style="font-family:Arial;font-weight:bold;margin-left:30px;font-size:10pt">
            <a href="#Intel vPro">Intel vPro</a>
            <a style="position:absolute;margin-left:40px" href="PrintPages/PrintvPro.html">
              <img border="0" src="images/PrintButton.png" />
            </a>
            <ol style="margin-top:5">
              <xsl:for-each select="identifiers/SectionName/alert">
                <xsl:if test="contains(@name, 'Intel vPro')">
                  <li style="margin-left:10pt;font-size:8pt">
                    <a>
                      <xsl:attribute name="href">#
                      <xsl:value-of select="@name" /></xsl:attribute>
                      <xsl:value-of select="@name" />
                    </a>
                  </li>
                </xsl:if>
              </xsl:for-each>
            </ol>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Above is my code sample.

The first xsl:if statement always fails and never shows the Agent Watcher text or print me button. Even if the section is filled out in the XML. If the section is there, the first xsl:if statement fails, but the second one, contained in the xsl:for-each shows the content. How do I get this to work.

I want to have it encompassing so that if the XML has content in the section it will put it up but if not it wont be empty content with a header or vice versa. Attaching sample XML to process.

<identifiers>
  <sectionname>
    <alert name="Agent Watcher Service Startup"></alert>
    <alert name="Agent Watcher Service Not Started"></alert>
    <alert name="Agent Watcher Service Uninstalled"></alert>
    <alert name="Agent Watcher File Deleted"></alert>
  </sectionname>
  <sectionname>
    <alert name="Intel vPro agentless discovery failure"></alert>
    <alert name="Intel vPro System Defense Remediation Alert"></alert>
    <alert name="Intel vPro Enhanced System Defense Remediation Alert"></alert>
    <alert name="Intel vPro Enhanced System Defense Alert"></alert>
  </sectionname>
</identifiers>

Blockquote

+1  A: 

I have a few other suggestions but you need to post the entire (relevant) XSLT before I can go on. At least the enclosing template is necessary.

EDIT: Here is my proposal for your stylesheet:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:tmp="http://tempuri.org"
  exclude-result-prefixes="tmp"
>
  <tmp:config>
    <tmp:alert label="Agent Watcher" link="PrintPages/PrintAW.html" />
    <tmp:alert label="Intel vPro" link="PrintPages/PrintvPro.html" />
  </tmp:config>

  <xsl:variable name="everyAlert" select="
    /identifiers/sectionname/alert
  " />

  <xsl:template match="/">
    <html>
      <body>
        <!-- 8< snip -->
        <div style="...">
          <div style="...">
            <xsl:text>Table of Contents - </xsl:text>
            <a style="..." href="PrintPages/PrintAll.html">
              <img border="0" src="images/PrintButton.png" />
            </a>
          </div>
          <xsl:for-each select="document('')/*/tmp:config/tmp:alert">
            <xsl:call-template name="section" />
          </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="section">
    <xsl:variable name="this" select="." />
    <xsl:variable name="alerts" select="
      $everyAlert[contains(@name, $this/@label)]
    " />

    <xsl:if test="$alerts">
      <div style="...">
        <a href="#{translate($this/@label, ' ', '_')}">
          <xsl:value-of select="$this/@label" />
        </a>
        <a style="..." href="{$this/@link}">
          <img border="0" src="images/PrintButton.png" />
        </a>
        <ol style="...">
          <xsl:for-each select="$alerts">
            <li style="...">
              <a href="#{@name}"><xsl:value-of select="@name" /></a>
            </li>
          </xsl:for-each>
        </ol>
      </div>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Key features:

  • efficient code reuse through a named template
  • printed sections are easily configurable
  • uses <xsl:text> elements to avoid unwanted whitespace in the output while retaining freedom to format the XSLT code properly
  • uses attribute literal notation (the curly braces {}) instead of verbose <xsl:attribute> elements
  • uses a temporary namespace to allow storing config data in the stylesheet itself
  • uses an <xsl:for-each> loop and the document() function to retrieve and work with that config data
  • the for-each makes use of the context to transport the current @label and @link so no <xsl:param> is necessary (the <xsl:template name="section"> runs in tmp:config/tmp:alert context, not in sectionname/alert context!)
  • uses a global variable ($everyAlert) to store all nodes for later use
Tomalak
Thanks, I was able to glean exactly what I needed from your answer. I'll be honest it was more than I needed, and at the moment, more than I could handle. BUT...You're revision did give me some excellent ideas and it is definitely great code structure and use. I'll be using this as a reference as I continue cleaning and reforming my xsl.Thanks a ton.Kothoga
Kevsteelio
Happy to help. If you still have a question about a certain part in the above code, don't be afraid to ask in another comment.
Tomalak