Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Selection/Extraction trouble ... please help Selection/Extraction trouble ... please helpTo: 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 | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
