Avatar of tjgquicken
tjgquicken
 asked on

XSLT processing text and element nodes

I'm having trouble figuring out how to write a template that can take as input a result tree that contains element nodes intermingled with text nodes and output the nodes in order, applying a template to each of the element nodes. I hope the code snippet I attached will make that clearer: I want it to print out:

lorem ipsum [lowercase alpha symbol] dolorem est
<!-- entity.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <txt>lorem ipsum <entity>alpha</entity> dolorem est</txt>
</root>
 
<!-- entity.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
 
<xsl:template match="root">
  <xsl:message terminate="no">-- root template --</xsl:message>
  <xsl:apply-templates select="txt"/>
</xsl:template>
 
<xsl:template match="txt">
  <xsl:message terminate="no">-- txt template --</xsl:message>
  <xsl:for-each select="*">
    <xsl:choose>
      <xsl:when test="text()">
        <xsl:copy/>
      </xsl:when>
      <xsl:when test="entity">
        <xsl:apply-templates select="entity"/>
      </xsl:when>
    </xsl:choose>
  </xsl:for-each>
  <xsl:apply-templates select="entity"/>
</xsl:template>
 
<xsl:template match="entity">
  <xsl:message terminate="no">-- entity template --</xsl:message>
  <xsl:text>&#945;</xsl:text>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

Web Languages and Standards

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gertone (Geert Bormans)

There is a built-in template that deals with the text()
If you want to control the txt/text() nodes, you can do it like this

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
       
        <xsl:template match="root">
            <xsl:message terminate="no">-- root template --</xsl:message>
            <xsl:apply-templates select="txt"/>
        </xsl:template>
       
        <xsl:template match="txt">
            <xsl:message terminate="no">-- txt template --</xsl:message>
            <xsl:apply-templates select="node()"/>
        </xsl:template>
       
        <xsl:template match="entity">
            <xsl:message terminate="no">-- entity template --</xsl:message>
            <xsl:text>&#945;</xsl:text>
        </xsl:template>

        <xsl:template match="txt/text()">
            <xsl:message terminate="no">- txt template, text() content -</xsl:message>
            <xsl:value-of select="."/>
        </xsl:template>
       
       
</xsl:stylesheet>
Gertone (Geert Bormans)

Note that the reporting with xsl:message doesn't make much sense.
There is a possibility that the XSLT processor works things in parallel.
So there is a good chance that you will see the messages appear in a different order than you would expect
and still the XSLT would render the correct result
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck