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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace an xml tag used inside text content of element

Status
Not open for further replies.

drewInJapan

Programmer
Sep 16, 2009
2
US
I've been beating my head against the wall for hours on this.
(note: I'm forced to use xslt 1.0 and I can't alter the way the xml being transformed is formatted)

Given the following xml doc:
Code:
<?xml version="1.0" encoding="utf-8"?>
<SEGMENTS>
 <HIT NO="1" >
  <FIELD NAME="title">
   <key>Ethics</key> in Your Org<sep/>
  </FIELD>
 </HIT>
 <HIT NO="2" >
  <FIELD NAME="title">
   Ways to Promote <key>Ethics</key><sep/>
  </FIELD>
 </HIT>
</SEGMENTS>

I have to iterate through and output the titles while replacing the 'key' tags with html 'span' tags and the 'sep' tags with text ellipses, such as the following:

Code:
<span class="keyword">Ethics</span> in Your Org...
Ways to Promote <span class="keyword">Ethics</span>...

I won't waste space showing the multiple failed paths I've taken up to now trying to solve this unless it might be of help.
Any kind souls out there have any suggestions?

Thanks,
-Drew
 
Like this.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]">
<xsl:eek:utput method="html" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="SEGMENTS/HIT/FIELD[@NAME='title']" />
</body>
</html>
</xsl:template>
<xsl:template match="FIELD[@NAME='title']">
<xsl:apply-templates select="text()|key|sep" mode="title" />
</xsl:template>
<xsl:template match="key" mode="title">
<span class="keyword"><xsl:value-of select="." /></span>
</xsl:template>
<xsl:template match="sep" mode="title">
<xsl:text>... </xsl:text>
</xsl:template>
<xsl:template match="text()" mode="title">
<xsl:value-of select="normalize-space()" />
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Thanks so much Tsuji!

This worked like a charm and was far cleaner than any of the solutions I came up with.

-Drew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top