Jump to content

Why is it selecting BOTH the element and next sibling?


javajoemorgan

Recommended Posts

Consider a sample XML:

<ANYTHING>  <ANYTHING-ELSE>    <DOC-HEAD>Title</DOC-HEAD>    <DOC-SUBHEAD>Sub Title</DOC-SUBHEAD>    <!-- way more stuff but no more DOC-HEADS -->  <ANYTHING-ELSE><ANYTHING>

I need to generically transform the contents of DOC-HEAD, under whatever initial elements that may contain it. I'm doing this:

<xsl:template match="/">     <h1><xsl:value-of select="//DOC-HEAD"/></h1>     <!-- all else --></xsl:template>

This actually works. However, later in the doc, it puts BOTH the contents of DOC-HEAD and DOC-SUBHEAD together, as in: TitleSub TitleThere are no generic templates (match="*"), nor templates maching DOC-SUBHEAD., and only this one place where it addresses DOC-HEAD.Confused Joe Morgan

Link to comment
Share on other sites

Somewhere below in the XSLT, you probably have <xsl:apply-templates/>.That element applies all templates from the point it is iniated on. Text nodes have a built in template too however.That is:

<xsl:template match="text()"><xsl:value-of select="."/></xsl:template>

In that essense, unless you somehow prevent XSLT from reaching a text node, it will "echo" the text node.There are two things you may do:1. (not reccomended) Override the text() template to make it do nothing, like:

<xsl:template match="text()"/>

This will effectively disable the repeating later on. This is a bad idea however, because you'll also have to manually value-of everthing you may ever need to output.2. (reccomended) create a new template for the needed <h1> element in the output, and/or limit the XSLT from reaching the text() node by using the "select" attribute of <xsl:apply-templates/>. Something like:<xsl:template match="/">

	 <xsl:apply-templates/></xsl:template><xsl:template match="DOC-HEAD"><h1><xsl:apply-templates/></h1></xsl:template><!-- Other <xsl:template/> elements that map certain other XML elements to certain (X)HTML elements -->

Link to comment
Share on other sites

Somewhere below in the XSLT, you probably have <xsl:apply-templates/>.
Well, I didn't want to overwhelm this with code... but I don't see where it is going wrong. Here is the entire XSL.
<?xml version="1.0"?><!-- =============================================================================This style sheet transforms Bulletin and Article data into a navigation menu================================================================================== --><xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:xsd="http://www.w3.org/2001/XMLSchema"    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp"    exclude-result-prefixes="#all">    <!-- =============================================================================================         This transform creates the navigation menu division for the document.    ================================================================================================== -->    <xsl:template match="/">            <p><xsl:value-of select="//DOC-HEAD"/></p>            <ul class="clsHeadFive">                 <li>HEADINGS</li>                 <li><a href="_top">ARTICLE BEGINNING</a></li>                 <ul>                      <xsl:apply-templates match="INFO-OBJ"/>                 </ul>            </ul>            <ul class="clsHeadFive">                 <li>FIGURES</li>                 <ul>                     <xsl:apply-templates match="FIGURE"/>                 </ul>            </ul>            <ul class="clsHeadFive">                 <li>TABLES</li>                 <ul>                     <xsl:apply-templates match="TABLE"/>                 </ul>            </ul>    </xsl:template>    <!-- =============================================================================================         Transforms INFO-OBJ elements.  These contain a nesting hierarchy in which each different         level is presented differently, thus the need for the count of ancestor INFO-OBJ elements.           The CSS file currently contains only five levels for this.    ================================================================================================== -->    <xsl:template match="INFO-OBJ">        <li class="clsHeadFive"><a href="#{@ID}"><xsl:value-of select="TITLE"/></a></li>        <xsl:if test="count(descendant::INFO-OBJ) > 0">            <ul>               <xsl:apply-templates select="INFO-OBJ"/>            </ul>        </xsl:if>    </xsl:template>    <!-- =============================================================================================         Creates a reference to a FIGURE, however, this opens the figure in a window rather than          having if reference a FIGURE within the doc    ================================================================================================== -->    <xsl:template match="FIGURE">        <li>            <xsl:choose>                <xsl:when test="GRAPHIC[@GRAPHICTYPE='GIF']">                    <a class="clsGraphicLink" href="{GRAPHIC/@GRAPHICFILEPATH}" target="_blank">                        Fig. <xsl:number format="1" level="any"/>: <xsl:value-of select="CAPTION"/>                    </a>                </xsl:when>                <xsl:when test="GRAPHIC[@GRAPHICTYPE='SVGZ']">                    <a class="clsGraphicLink" href="java script:showSVG('{GRAPHIC/@GRAPHICFILEPATH}'); return false;">                        Fig. <xsl:number format="1" level="any"/>: <xsl:value-of select="CAPTION"/>                    </a>                </xsl:when>            </xsl:choose>        </li>    </xsl:template>    <!-- =============================================================================================          Creates a reference to a TABLE in the doc    ================================================================================================== -->    <xsl:template match="TABLE">        <li><a href="#{@ID}"><xsl:value-of select="TITLE"/></a></li>    </xsl:template></xsl:stylesheet>

The objective is to create a NAV menu such that all article headings (INFO-OBJ) are first, tables (TABLE) are next, and images (FIGURE) are last. So the intent is to transform all INFO-OBJ using their TITLE element as text. These form a nested hierarchy in the document and therefore must have the same nesting in the nav. The tables and figures are not hierarchical and thus are simply listed under main "TABLES" and "FIGURES" headings respectively.Joe

Link to comment
Share on other sites

The attribute on xsl:apply-templates for selecting a template to execute is "select", not "match". You're probably using an XSLT 1.0 processor but you've declared to use XSLT 2.0. Because of that, your XSLT 1.0 processor goes into forwards compatible mode in which case it ignores this wrong attribute. If it was an XSLT 2.0 processor, it should have alerted you of your error.Try it like that:

<?xml version="1.0"?><!-- =============================================================================This style sheet transforms Bulletin and Article data into a navigation menu================================================================================== --><xsl:stylesheet version="1.0" 	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	xmlns:xsd="http://www.w3.org/2001/XMLSchema"	xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"	xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"	xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp"	exclude-result-prefixes="#all">	<!-- =============================================================================================		 This transform creates the navigation menu division for the document.	================================================================================================== -->	<xsl:template match="/">			<p><xsl:value-of select="//DOC-HEAD"/></p>			<ul class="clsHeadFive">				 <li>HEADINGS</li>				 <li><a href="_top">ARTICLE BEGINNING</a></li>				 <ul>					  <xsl:apply-templates select="INFO-OBJ"/>				 </ul>			</ul>			<ul class="clsHeadFive">				 <li>FIGURES</li>				 <ul>					 <xsl:apply-templates select="FIGURE"/>				 </ul>			</ul>			<ul class="clsHeadFive">				 <li>TABLES</li>				 <ul>					 <xsl:apply-templates select="TABLE"/>				 </ul>			</ul>	</xsl:template>	<!-- =============================================================================================		 Transforms INFO-OBJ elements.  These contain a nesting hierarchy in which each different		 level is presented differently, thus the need for the count of ancestor INFO-OBJ elements.  		 The CSS file currently contains only five levels for this.	================================================================================================== -->	<xsl:template match="INFO-OBJ">		<li class="clsHeadFive"><a href="#{@ID}"><xsl:value-of select="TITLE"/></a></li>		<xsl:if test="count(descendant::INFO-OBJ) > 0">			<ul>			   <xsl:apply-templates select="INFO-OBJ"/>			</ul>		</xsl:if>	</xsl:template>	<!-- =============================================================================================		 Creates a reference to a FIGURE, however, this opens the figure in a window rather than 		 having if reference a FIGURE within the doc	================================================================================================== -->	<xsl:template match="FIGURE">		<li>			<xsl:choose>				<xsl:when test="GRAPHIC[@GRAPHICTYPE='GIF']">					<a class="clsGraphicLink" href="{GRAPHIC/@GRAPHICFILEPATH}" target="_blank">						Fig. <xsl:number format="1" level="any"/>: <xsl:value-of select="CAPTION"/>					</a>				</xsl:when>				<xsl:when test="GRAPHIC[@GRAPHICTYPE='SVGZ']">					<a class="clsGraphicLink" href="java script:showSVG('{GRAPHIC/@GRAPHICFILEPATH}'); return false;">						Fig. <xsl:number format="1" level="any"/>: <xsl:value-of select="CAPTION"/>					</a>				</xsl:when>			</xsl:choose>		</li>	</xsl:template>	<!-- ============================================================================================= 		 Creates a reference to a TABLE in the doc	================================================================================================== -->	<xsl:template match="TABLE">		<li><a href="#{@ID}"><xsl:value-of select="TITLE"/></a></li>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...