Re: [xsl] defining nodes to apply template to

Subject: Re: [xsl] defining nodes to apply template to
From: ADAM PATRICK <adampatrick@xxxxxxxxxxxxxx>
Date: Wed, 10 Aug 2005 15:46:41 +0100 (BST)
final post!!!

what i have is that some lines need to be parent nodes
and some lines need to be child nodes...

below are the files so far...thanks.

excerpt from xml file...

<data>;</data>
<data>;	Traceability  	: 		BCD123456</data>
<data>;		 	  		R[123]/P/C</data>
<data>;					R[456]/A</data>
<data>;					BCD567890</data>
<data>;					R[456]</data>
<data>;					BCD567999</data>
<data>;					R[123]/A</data>
<data>;</data>

xsl file...

<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
	version="1.0">
	<xsl:variable name="REQ_START">R[</xsl:variable>
	<xsl:variable name="REQ_END">]</xsl:variable>
	<xsl:variable name="TRACE_START">; Traceability :
BCD</xsl:variable>
	<xsl:variable name="TRACE_END">; IDENTITY :
</xsl:variable>
	<xsl:variable name="MAX_REQ_LENGTH">8</xsl:variable>
	<xsl:variable name="DOC_TAG">BCD</xsl:variable>
	<xsl:variable name="PARTIAL_TAG">/P</xsl:variable>
	<xsl:variable name="COMPONENT_TAG">/C</xsl:variable>
	<xsl:variable name="ANALYSIS_TAG">/A</xsl:variable>
	<xsl:param name="abc_DOC_REF"/>
	
	<xsl:output indent="yes"/>

	<xsl:template match="abc">
		<abc>
		<abc_doc abc_doc_ref="{$abc_DOC_REF}">
			<xsl:apply-templates select="data"/>
		</abc_doc>
		</abc>
	</xsl:template>
	
	<xsl:template
match="data[(following-sibling::data)[contains(normalize-space(.),';
Traceability : BCD')]]"/>
	<xsl:template
match="data[(.|preceding-sibling::data)[contains(normalize-space(.),';
IDENTITY : ')]]"/>
	
	<xsl:template match="data">
		<xsl:choose>
			<xsl:when test="contains(.,'BCD')">
				<parent_doc_ref>
				<xsl:choose>
					<xsl:when test="contains(normalize-space(.),';
Traceability : ')">
						<xsl:value-of
select="substring-after(normalize-space(.),'Traceability
: ')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of
select="substring-after(normalize-space(.),' ')"/>
					</xsl:otherwise>
				</xsl:choose>
				</parent_doc_ref>
			</xsl:when>
			<xsl:when test="contains(.,$REQ_START) and
contains(.,$REQ_END)">
				<xsl:call-template name="Format">
					<xsl:with-param name="STR" select="."/>
				</xsl:call-template>
			</xsl:when>
		</xsl:choose>			
	</xsl:template>
	
	<xsl:template name="Format">
	<xsl:param name="STR"/>
		<xsl:if test="contains($STR,$REQ_START)">
			<req_id>
				<xsl:value-of
select="substring-before(substring-after(normalize-space($STR),$REQ_START),$REQ_END)"/>
			<xsl:choose>
				<xsl:when
test="contains(substring-after(normalize-space($STR),$REQ_END),$PARTIAL_TAG)">
				<abc_partial>1</abc_partial>
				</xsl:when>
				<xsl:otherwise>
				<abc_partial>0</abc_partial>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:choose>
				<xsl:when
test="contains(substring-after(normalize-space($STR),$REQ_END),$COMPONENT_TAG)">
				<abc_component>1</abc_component>
				</xsl:when>
				<xsl:otherwise>
				<abc_component>0</abc_component>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:choose>
				<xsl:when
test="contains(substring-after(normalize-space($STR),$REQ_END),$ANALYSIS_TAG)">
				<abc_analysis>1</abc_analysis>
				</xsl:when>
				<xsl:otherwise>
				<abc_analysis>0</abc_analysis>
				</xsl:otherwise>
			</xsl:choose>
			</req_id>
		</xsl:if>
	</xsl:template>		
	
</xsl:stylesheet>

...current output

<?xml version="1.0" encoding="utf-8"?>
<abc>
   <abc_doc abc_doc_ref="">
      <parent_doc_ref>BCD123456</parent_doc_ref>
      <req_id>123<abc_partial>1</abc_partial>
         <abc_component>1</abc_component>
         <abc_analysis>0</abc_analysis>
      </req_id>
      <req_id>456<abc_partial>0</abc_partial>
         <abc_component>0</abc_component>
         <abc_analysis>1</abc_analysis>
      </req_id>
      <parent_doc_ref>BCD567890</parent_doc_ref>
      <req_id>456<abc_partial>0</abc_partial>
         <abc_component>0</abc_component>
         <abc_analysis>0</abc_analysis>
      </req_id>
      <parent_doc_ref>BCD567999</parent_doc_ref>
      <req_id>123<abc_partial>0</abc_partial>
         <abc_component>0</abc_component>
         <abc_analysis>1</abc_analysis>
      </req_id>
   </abc_doc>
</abc>

...desired output


<?xml version="1.0" encoding="utf-8"?>
<abc>
   <abc_doc abc_doc_ref="">
      <parent_doc_ref>BCD123456
	      <req_id>123<abc_partial>1</abc_partial>
		 <abc_component>1</abc_component>
		 <abc_analysis>0</abc_analysis>
	      </req_id>
	      <req_id>456<abc_partial>0</abc_partial>
		 <abc_component>0</abc_component>
		 <abc_analysis>1</abc_analysis>
	      </req_id>
      </parent_doc_ref>
      <parent_doc_ref>BCD567890
      <req_id>456<abc_partial>0</abc_partial>
         <abc_component>0</abc_component>
         <abc_analysis>0</abc_analysis>
      </req_id>
      </parent_doc_ref>
      <parent_doc_ref>BCD567999</parent_doc_ref>
      <req_id>123<abc_partial>0</abc_partial>
         <abc_component>0</abc_component>
         <abc_analysis>1</abc_analysis>
      </req_id>
      </parent_doc_ref>
   </abc_doc>
</abc>

Current Thread