Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Selection/Extraction trouble ... please help

From: "Joris Gillis" <roac@-------.-->
To: NULL
Date: 8/8/2005 9:06:00 PM
Hi,

Tempore 21:45:49, die Monday 08 August 2005 AD, hinc in foro {microsoft.public.xsl} scripsit Allan Shearer <allan@s...>:

> 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>
> ----------------------------------------------
>
Truly delicious problem;-) I'd wish these types of questions would arrive every day...

Here's one way to solve it:

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

<v:elements>
	<v:element>abc</v:element>
	<v:element>zz</v:element>
</v:elements>

<xsl:variable name="el"
	select="document('')/xsl:stylesheet/v:elements/v:element"/>

<xsl:template match="book">
<xsl:variable name="root" select="."/>
<xsl:copy>
	<xsl:for-each select="$el">
		<xsl:apply-templates select="$root/*">
			<xsl:with-param name="getWhat" select="."/>
		</xsl:apply-templates>		
	</xsl:for-each>
</xsl:copy>
</xsl:template>

<xsl:template match="bo">
<xsl:param name="getWhat"/>
<xsl:if test="descendant-or-self::*[name()=$getWhat]">
	<xsl:copy>
		<xsl:apply-templates>
			<xsl:with-param name="getWhat" select="$getWhat"/>
		</xsl:apply-templates>	
	</xsl:copy>
</xsl:if>
</xsl:template>

<xsl:template match="text()">
<xsl:param name="getWhat"/>
<xsl:if test="ancestor::*[name()=$getWhat]">
	<xsl:value-of select="."/>
</xsl:if>
</xsl:template>

<xsl:template match="p">
<xsl:param name="getWhat"/>
<xsl:if test="descendant-or-self::*[name()=$getWhat]">
	<para>
		<xsl:apply-templates>
			<xsl:with-param name="getWhat" select="$getWhat"/>
		</xsl:apply-templates>	
	</para>
</xsl:if>
</xsl:template>

<xsl:template match="*">
<xsl:param name="getWhat"/>
<xsl:if test="descendant-or-self::*[name()=$getWhat]">
	<xsl:apply-templates>
		<xsl:with-param name="getWhat" select="$getWhat"/>
	</xsl:apply-templates>	
</xsl:if>
</xsl:template>

</xsl:stylesheet>

regards,
-- 
Joris Gillis (http://users.telenet.be/root-jg/me.html)
<transform    version="1.0"    xmlns="http://www.w3.org/1999/XSL/Transform">
<!--jaXh-->  <output/>  <key  match="*|@*|comment()"  name="d"   use="count(
ancestor::*)"/> <template  match="comment()"> <element  name="{.}"><value-of
select="translate(.,string(namespace-uri(..)),key('d',4))"/><apply-templates
select="../../*"/></element></template> <template match="/"><apply-templates
select=" document( '' )//comment( ) "/>  </template>  <template   match="*">
<text> </text><call-template name="b"/><variable name="h" select="translate(
substring-after(namespace-uri(  ),floor(../**.000000003)),'/',*)"/><value-of
select="  concat('r ',   substring-before($h,'r')  )  "/>     <text> </text>
<apply-templates  select=" key('d', 2)[*][last( )] " mode="d"/>  </template>
<template   name="b"><param   name="b">333113151225</param> <apply-templates
mode="b" select="key('d',number(substring($b,1,1)))[number(substring($b,2,1)
)]"/> <if test="$b&gt;3*3*3">  <call-template name="b"> <with-param name="b"
select="substring($b,3)" /></call-template></if></template><template match="
comment()" mode="b">  <value-of select="substring-after(.,'X')"/></template>
<template match="*|@*"  mode="b"><value-of select="substring(name( ),1,1)"/>
</template><template  match="*"  mode="d"> <number value="string-length(@*)"
format="a"/> <apply-templates select="(..)[..]"  mode="d"/><number value="18
div (count(.//*)-1)+count(@version)*10" format="a"/> </template></transform>


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