Jump to content

Moving instances of an elements to be below instances of another element


yoshimura

Recommended Posts

I'm trying to word this correctly,Every instance of element x must be placed right below the nearest element y.For example: A subheading is either above or below the heading in a book. I need the subheading to always be beneath the heading.Help would be much appreciated.

Link to comment
Share on other sites

Consider to post a sample of the input and the corresponding output you want to create with XSLT. Also explain whether you want to use XSLT 1.0 or 2.0, as 2.0 has stuff like for-each-group group-starting-with/ending-with that could help in your case.
If XSLT 2.0 is more helpful, I'll go with that version. How do I tell the XSLT to use it though?Input:
<Book>	<Article>		<Subhead>Title </Subhead>		<Heading>Title</Heading>		<Body>Text</Body>   	</Article>

Sometimes the <Subhead> is above the <Heading>. There are multiple instances of both positions (above and below) throughout the book.Desired output:

<Book>	<Article>		<Heading>Title</Heading>		<Subhead>Title </Subhead>		<Body>Text</Body>   	</Article>

Link to comment
Share on other sites

If you want to use XSLT 2.0 then you need to use an XSLT 2.0 processor like Saxon 9 or like AltovaXML tools:

<xsl:template match="@* | node()">  <xsl:copy>	<xsl:apply-templates select="@*, node()"/>  </xsl:copy></xsl:template><xsl:template match="Article">  <xsl:copy>	<xsl:apply-templates select="@*, Heading, Subhead, Body"/>  </xsl:copy></xsl:template>

should suffice with XSLT 2.0.With 1.0 you could use

<xsl:template match="@* | node()">  <xsl:copy>	<xsl:apply-templates select="@* | node()"/>  </xsl:copy></xsl:template><xsl:template match="Article">  <xsl:copy>	<xsl:apply-templates select="@*"/>	<xsl:apply-templates select="Heading"/>	<xsl:apply-templates select="Subhead"/>	<xsl:apply-templates select="Body"/>  </xsl:copy></xsl:template>

Link to comment
Share on other sites

Hey Martin,I chose Saxon-EE 9.2.1.2 as the processor (in Oxygen).However, I think I should have elaborated more on my code, as the solution you provided isn't compatible with it.Here's what I have at the top of my XSLT.

<xsl:template match="/">		<html>			<head>				<title>Booktitle</title>			</head>			<body>				<xsl:apply-templates/>			</body>		</html>	</xsl:template>

<Head> and <Subhead> are also the children of many other elements other than <Article>Broader example of the structure:

<Book>		<Article>			<Subhead>				Subhead Text			</Subhead>			<Heading>				Heading Text			</Heading>			<Tags in between>				Body texts of the article			</Tags in between>		</Article>		<Article2>			<Subhead>				Subhead Text			</Subhead>			<Heading>				Heading Text			</Heading>			<Tags in between>				Body texts of the article			</Tags in between>		</Article2>		<Article3> (ad infinitum)			<Subhead>				Subhead Text			</Subhead>			<Heading>				Heading Text			</Heading>			<Tags in between>				Body texts of the article			</Tags in between>		</Article2></Book>

Link to comment
Share on other sites

Thanks for your help Martin,Solution:

<!-- Match everything in the root --><xsl:template match="/">  <xsl:copy>	<xsl:apply-templates />  </xsl:copy> </xsl:template><xsl:template match="Article|Article2|Article3">	 <xsl:choose>		  <xsl:when test="./Subhead and ./Heading">			   <xsl:copy>					<xsl:apply-templates select="Heading" />					<xsl:apply-templates select="Subhead" />					<xsl:apply-templates mode="noHeading" />			   </xsl:copy>		  </xsl:when>		  <xsl:otherwise>	 			   <xsl:copy>					<xsl:apply-templates />			   </xsl:copy>		  </xsl:otherwise>	 	 </xsl:choose></xsl:template><!-- Match everything except Heading or Subhead --><xsl:template match="@*|node()" mode="noHeading" ><xsl:if test="name() != 'Heading' and name() != 'Subhead'"><xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy><xsl:text></xsl:text></xsl:if></xsl:template><!-- Match everything --><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy><xsl:text></xsl:text></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...