Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Selection/Extraction trouble ... please help

From: Allan Shearer <allan@-------.-->
To: NULL
Date: 8/8/2005 3:46:00 PM
Hi Everyone

I'm having some trouble wrapping my mind around the following ... perhaps someone can help me.

Here's a sample of what I have (input), and what I want (output):

--------------------INPUT--------------------
<?xml version="1.0" encoding="UTF-8"?>
<!--only text within <abc> and <zz> to be extracted-->
<book>
	<p>Ignore me.<bo>Ignore me.</bo></p>
	<p>Ignore me.<bo><zz>ZZ Extract #1 </zz>Ignore me.</bo></p>
	<p>Ignore me.<abc>ABC Extract #1 </abc><bo>Ignore me.<abc>ABC Extract #2 </abc></bo></p>
	<p>Ignore me.<zz>ZZ Extract #2 </zz><bo>Ignore me.<abc>ABC Extract #3 </abc></bo></p>
	<p>Ignore me.<bo>Ignore me.</bo></p>
</book>
----------------------------------------------

--------------------OUTPUT--------------------
<?xml version="1.0" encoding="UTF-16"?>
<para>ABC Extract #1 <bo>ABC Extract #2 </bo></para>
<para><bo>ABC Extract #3 </bo></para>
<para><bo>ZZ Extract #1 </bo></para>
<para>ZZ Extract #2 </para>
----------------------------------------------

Notice:

1. ONLY 'abc' and 'zz' elements are extracted
2. All 'abc' are extracted first... then all 'zz'
3. Any surrounding elements are retained, such as <bo>
4. While I only have <p> shown as the parent element, <abc> and <zz> will be found in many more parents, such as <list/item>, 
<table/tblbody/tblrow/tblcell>, etc.....  Once I find a solution to this basic structure, I can apply it across them all - I'm sure.

Here's one of the many XSLT routines I've tried - had it working on very simple examples - but more complex/nested examples like 
the one above have totally thrown me for a loop!  I can't figure it out (read: mental-block!).

Please note: you'll quite likely find that there's a MUCH (MUCH) easier way to do the following - and please feel free to point 
that out.  Don't hold any punches.   :)

----------------------XSLT----------------------

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

	<xsl:template match="/">
		<!--First, extract all <abc> elements-->
		<xsl:apply-templates select="//*[abc]"/>
		<!--Next, extract all <zz> elements-->
		<xsl:apply-templates select="//*[zz]"/>
	</xsl:template>
	
	<xsl:template match="p">
		<xsl:if test=".//abc">
			<para>
				<xsl:apply-templates select=".//abc">
					<xsl:with-param name="getWhat">
						<xsl:text>abc</xsl:text>
					</xsl:with-param>
				</xsl:apply-templates>
			</para>
		</xsl:if>
		<xsl:if test=".//zz">
			<para>
				<xsl:apply-templates select=".//zz">
					<xsl:with-param name="getWhat">
						<xsl:text>zz</xsl:text>
					</xsl:with-param>
				</xsl:apply-templates>
			</para>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="bo">
	<xsl:param name="getWhat"/>
		<xsl:choose>
			<xsl:when test="$getWhat='abc'">
				<bo>
					<xsl:apply-templates select=".//abc">
						<xsl:with-param name="getWhat">
							<xsl:text>abc</xsl:text>
						</xsl:with-param>
					</xsl:apply-templates>
				</bo>
			</xsl:when>
			<xsl:when test="$getWhat='zz'">
				<bo>
					<xsl:apply-templates select=".//abc">
						<xsl:with-param name="getWhat">
							<xsl:text>zz</xsl:text>
						</xsl:with-param>
					</xsl:apply-templates>
				</bo>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="abc">
	<xsl:param name="getWhat"/>
		<xsl:choose>
			<xsl:when test="$getWhat='abc'">
				<xsl:apply-templates>
					<xsl:with-param name="getWhat">
						<xsl:text>abc</xsl:text>
					</xsl:with-param>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:when test="$getWhat='zz'">
				<!-- do nothing! -->
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="zz">
	<xsl:param name="getWhat"/>
		<xsl:choose>
			<xsl:when test="$getWhat='abc'">
				<!-- do nothing! -->
			</xsl:when>
			<xsl:when test="$getWhat='zz'">
				<xsl:apply-templates>
					<xsl:with-param name="getWhat">
						<xsl:text>zz</xsl:text>
					</xsl:with-param>
				</xsl:apply-templates>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
----------------------------------------------

Thanks in advance!

Yours,

Allan Shearer
allanATshearerDOTca


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent