Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substring for xml tree ? 1

Status
Not open for further replies.

sheila11

Programmer
Dec 27, 2000
251
US
Hi all,

One element in my xml document may possibly contain a <subscript> or <bold> tags. I need to parse the content of this element based on a certain character.
e.g:

<para>This is an <bold>example</bold> sentence | borrowed from my own data.</para>

If I use
<xsl:copy-of select="substring-before(.,'|')"/>

I get the first part of the content of <para>, but without the <bold> tags.

How can I get the content with any tags included ?

TIA,
Sheila
 
This will work if the '|' doesn't ever occur with a child element of <para>:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="para">
    <xsl:call-template name="getText">
      <xsl:with-param name="section" select="1"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="getText">
    <xsl:param name="section"/>
    <xsl:variable name="text" select="(*|text())[position() = $section]"/>
    <xsl:choose>
      <xsl:when test="contains($text, '|')">
        <xsl:value-of select="substring-before($text, '|')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$text"/>
        <xsl:call-template name="getText">
          <xsl:with-param name="section" select="$section + 1"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top