Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 25th, 2007, 12:16 AM
Authorized User
 
Join Date: May 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default need to match string dynamically

hi!
   below is partial xslt file.This template works perfectly fine but i want to revise it in a way that
pattern of

<xsl:element name="DIV">
         <xsl:attribute name="DEPTH">1</xsl:attribute>
          <xsl:element name="HEADER">OBJECTIVE</xsl:element>
              <xsl:element name="P">
                    <xsl:value-of select="text()" />
           </xsl:element>
           </xsl:element>


should not repeate every time.I want to eliminate this repetation. note: string "BACKGROUND,METHODS,..... could be change or added later"
I need suggestion to do this.


[[[[[[[[[[[partial xslt code: need to revise]]]]]]]]]]]]]]]]]]]]]]

<xsl:template match="xhtml:table/xhtml:tbody/xhtml:tr/xhtml:td">
     <xsl:for-each select="xhtml:p">
       <xsl:variable name="textcontent" select="text()" />
        <xsl:choose>
        <xsl:when test="contains($textcontent,'OBJECTIVE')">
            <xsl:element name="DIV">
         <xsl:attribute name="DEPTH">1</xsl:attribute>
          <xsl:element name="HEADER">OBJECTIVE</xsl:element>
              <xsl:element name="P">
                    <xsl:value-of select="text()" />
           </xsl:element>
           </xsl:element>
          </xsl:when>

           <xsl:when test="contains($textcontent,'BACKGROUND')">
         <xsl:element name="DIV">
         <xsl:attribute name="DEPTH">1</xsl:attribute>
         <xsl:element name="HEADER">BACKGROUND</xsl:element>
         <xsl:element name="P">
         <xsl:value-of select="text()" />
         </xsl:element>
         </xsl:element>
                  </xsl:when>

                 <xsl:when test="contains($textcontent,'METHODS')">
                  <xsl:element name="DIV">
               <xsl:attribute name="DEPTH">1</xsl:attribute>
                 <xsl:element name="HEADER">METHODS</xsl:element>
                  <xsl:element name="P">
                <xsl:value-of select="text()" />
                    </xsl:element>
                     </xsl:element>
                          </xsl:when>

        <xsl:when test="contains($textcontent,'CONCLUSIONS')">
               <xsl:element name="DIV">
             <xsl:attribute name="DEPTH">1</xsl:attribute>
            <xsl:element name="HEADER">CONCLUSIONS</xsl:element>
                 <xsl:element name="P">
                 <xsl:value-of select="text()" />
                  </xsl:element>
                    </xsl:element>
                  </xsl:when>

          <xsl:otherwise></xsl:otherwise>
          </xsl:choose>
      </xsl:for-each>
   </xsl:template>

 
Old May 25th, 2007, 03:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You could cut it down a fair bit by using literal result elements, and putting the stuff that doesn't vary outside the xsl:choose:
<xsl:for-each select="xhtml:p">
<xsl:variable...>
<DIV DEPTH="1">
  <HEADER>
    <xsl:choose>
     <xsl:when test="contains($textcontent,'OBJECTIVE')"
       >OBJECTIVE</xsl:when>
     <xsl:when test="contains($textcontent,'BACKGROUND')"
       >BACGROUND</xsl:when>

   ....
</DIV>


Beyond that, you can call a named template with parameters but it wouldn't reduce the number of lines of code very much.

Your logic can probably be simplified if one knows more about the source document, for example can a p element contain more than one of these strings, and is the order of testing them important? The following 2.0 code isn't equivalent to yours, but might give the right result depending on the data:

<xsl:for-each select="xhtml:p">
<xsl:variable name="textcontent" select="text()"/>
<DIV DEPTH="1">
  <HEADER>
    <xsl:for-each select="'OBJECTIVE', 'BACKGROUND', 'METHODS'...">
      <xsl:if test="contains($textcontent, .)">
        <xsl:value-of select="."/>

   ....
</DIV>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 25th, 2007, 08:49 AM
Authorized User
 
Join Date: May 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Michael
      It works ..thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Search for a String Match combo Box tsadok VB Databases Basics 0 January 27th, 2008 02:24 AM
STRING EXACT MATCH ricespn Beginning VB 6 6 November 12th, 2007 01:18 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
match string in more than one line using Regex suman9730 General .NET 0 October 24th, 2006 02:27 AM
Connection String Dynamically ~Bean~ Classic ASP Databases 2 May 28th, 2005 02:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.