<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
	<xsl:output method="xml" indent="yes" encoding="UTF-8" />
	
	<xsl:template match="document">
		<fo:root>
			<fo:layout-master-set>
				<!-- This sets up the page dimensions, margins, and layout -->
				<fo:simple-page-master master-name="A4-portrait" page-height="29.7cm" page-width="21.0cm" margin="2cm">
					<fo:region-body margin-top="1.6cm" margin-bottom="1.6cm"/>
					<fo:region-before extent="1.5cm"/>
					<fo:region-after extent="1.5cm"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="A4-portrait">
				<fo:static-content flow-name="xsl-region-before">
					<!-- Here we are creating a header for the page with a grey background -->
					<fo:block background-color="grey" text-align="center" font-size="24pt" font-weight="bold"></fo:block>
				</fo:static-content>
				<fo:static-content flow-name="xsl-region-after">					
					<fo:block border-top-style="solid" border-top-width="thin" padding-top="5pt" font-size="10pt" text-align="center"><fo:inline font-weight="bold">University of Arkansas Cooperative Extension Service</fo:inline> 2301 S. University Ave, Little Rock, AR 72204 </fo:block>
					<fo:block font-size="10pt" text-align="center">(501) 671-2000</fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<fo:block>
						<xsl:apply-templates select="content/maincontent"/>AFter events
						<xsl:apply-templates select="content/bottomcontent"/>bottom
						<xsl:apply-templates select="content"/>
						<xsl:apply-templates select="news"/>news
						
						<xsl:apply-templates select="bottomcontent"/>
						<xsl:apply-templates select="events"/>
						<xsl:apply-templates select="resources"/>
						<xsl:apply-templates select="images"/>
					</fo:block>
				</fo:flow>
				</fo:page-sequence>
		</fo:root>
	</xsl:template>
	
	<!-- This takes the title from the PCF and styles the text with 18pt and spacing afterwards -->
	<xsl:template match="title">
		<fo:block font-size="18pt" space-after="0.5cm">
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>
	
	

	<!-- This matches the content region of the PCF -->
	<xsl:template match="maincontent">
		<fo:block>Maincontent block
			<xsl:apply-templates/>Maincontent after template
		</fo:block>
	</xsl:template>
	
	
	<!-- This matches the bottom content region of the PCF -->
	<xsl:template match="bottomcontent">
		<fo:block>
			<xsl:apply-templates/>
		</fo:block>
	</xsl:template>
	
	
	<!-- When the XSL finds a paragraph tag it will place a 0.5 centimeter space beneath each one -->
	<xsl:template match="p">
		<fo:block space-after="0.5cm">			
			<xsl:apply-templates/>(This is a paragraph P region)
		</fo:block>
	</xsl:template>
	
	<!-- When the XSL finds a table tag it will place a 0.5 centimeter space beneath each one bbridges-->
	<xsl:template match="table">
		<fo:block space-after="0.5cm">
			<xsl:apply-templates/>(This is a table region)
		</fo:block>
	</xsl:template>
	
	<!-- When the XSL finds an image it use the formatting below to style it -->
	<xsl:template match="img">
		<fo:block space-after="0.5cm">
			<xsl:apply-templates/>(This is an image region)
		</fo:block>
		<!--<fo:external-graphic src="https://www.uaex.uada.edu/_resources/images/logo.png" alt="University of Arkansas, Division of Agriculture, Research &amp; Extension, University of Arkansas System" width="553" height="67" />(This is an image section)-->
			
	</xsl:template>
	
	<xsl:template match="a">
	  <xsl:choose>
		  
	    <xsl:when test="@name">
	      <xsl:if test="not(name(following-sibling::*[1]) = 'h1')">
	        <fo:block line-height="0" space-after="0pt" 
	          font-size="0pt" id="{@name}"/>
	      </xsl:if>
	    </xsl:when>
		  
	    <xsl:when test="@href">
	      <fo:basic-link>
	        <xsl:choose>
	          <xsl:when test="starts-with(@href, '#')">
	            <xsl:attribute name="internal-destination">
	              <xsl:value-of select="substring(@href, 2)"/>
	            </xsl:attribute>
	          </xsl:when>
	          <xsl:otherwise>
	            <xsl:attribute name="external-destination">
	              <xsl:value-of select="@href"/>
	            </xsl:attribute>
	          </xsl:otherwise>
	        </xsl:choose>
	        <!--<xsl:apply-templates select="*|text()"/>-->
	      </fo:basic-link>
	      <xsl:if test="starts-with(@href, '#')">
	        <xsl:text> on page </xsl:text>
	        <fo:page-number-citation ref-id="{substring(@href, 2)}"/>
	      </xsl:if>
	    </xsl:when>
		  
		  
	  </xsl:choose>
	</xsl:template>

	
	<!-- When the XSL finds an image it use the formatting below to style it -->
	<xsl:template match="img">
		<xsl:param name="path_from_root" />

		<xsl:variable name="src">
			<xsl:choose>
				<xsl:when test="substring-before(@src,'://') = 'http'"><xsl:value-of select="@src" /></xsl:when>
				<xsl:otherwise>
					<xsl:choose>
						<xsl:when test="substring(@src, 1, 1) != '/' ">http://www.uaex.uada.edu<xsl:value-of select="$path_from_root" />/<xsl:value-of select="@src" /></xsl:when>
						<xsl:otherwise>http://www.uaex.uada.edu<xsl:value-of select="@src" /></xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:variable name="width">
			<xsl:if test="@width">
				<xsl:choose>
					<xsl:when test="@width > 500">500px</xsl:when>
					<xsl:otherwise><xsl:value-of select="@width" />px</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		</xsl:variable>

		<xsl:variable name="height">
			<xsl:if test="@height"><xsl:value-of select="@height" />px</xsl:if>
		</xsl:variable>

		
		<fo:external-graphic
			src="{$src}"
			scaling="uniform"
			scaling-method="integer-pixels"
			content-width="{$width}"
			width="{$width}"
			content-height="{$height}"
			height="{$height}" />
			
	</xsl:template>
	
	<!-- Each line break will have a 0.5 centimeter space beneath it -->
	<xsl:template match="br">
		<fo:block space-after="0.5cm" />
	</xsl:template>
	
	
	<fo:root>
   <fo:layout-master-set>
      <fo:simple-page-master>
         <fo:region-body/>
         <fo:region-before/>
         <fo:region-after/>
         <fo:region-start/>
         <fo:region-end/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:declarations>
      <fo:color-profile/>
   </fo:declarations>
   <fo:bookmark-tree>
      <fo:bookmark>
         <fo:bookmark-title>Any text</fo:bookmark-title>
         <fo:bookmark>...
         </fo:bookmark>
      </fo:bookmark>
   </fo:bookmark-tree>
   <fo:page-sequence>
      <fo:title>Any text, intermingled with:
         <fo:bidi-override>Any text, intermingled with:...
         </fo:bidi-override>
      </fo:title>
      <fo:folio-prefix>Any text, intermingled with:
         <fo:bidi-override>Any text, intermingled with:...
         </fo:bidi-override>
      </fo:folio-prefix>
      <fo:folio-suffix>Any text, intermingled with:
         <fo:bidi-override>Any text, intermingled with:...
         </fo:bidi-override>
      </fo:folio-suffix>
      <fo:static-content>
         <fo:block>Any text, intermingled with:...
         </fo:block>
      </fo:static-content>
      <fo:flow>
         <fo:block>Any text, intermingled with:...
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
  
</xsl:stylesheet>
