Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:17:06+00:00 2026-05-16T18:17:06+00:00

I’m trying to write a template that will grab a mixture of text nodes

  • 0

I’m trying to write a template that will grab a mixture of text nodes and elements within a parent element and create a new node. I’ve done a lot of searching and couldn’t find what I was looking for…so hopefully I’m not asking to basic a question.

Here is a sample of xml I want to transform:

<?xml version="1.0"?>
<root>
 <para>Here is some text that will ask users to enter a <rule-line/> [<emph type="it">date</emph>], and maybe their <rule-line/> [<emph type="it">name</emph>]. The text could come in different [<emph type="it">order</emph>] <rule-line/>, and their could be any number of instances.</para>

</root>

I want to group the bracketed text and the rule into a new element like so:

<entry>[<emph type"it">date</emph>]</entry>

I have a template that can identify the text I want to change, and I can change it, but I don’t know how to add the text I want to the result tree and omit the old text.

Here are the relevant templates:

<xsl:template match="para">
 <xsl:for-each select="* | text()">
  <xsl:choose>

   <xsl:when test="self::rule-line and following-sibling::node()[1][starts-with(., ' [')] and string(node-name(following-sibling::node()[2])) = 'emph' and following-sibling::node()[3][starts-with(., ']')]">
    <xsl:comment>made match</xsl:comment>
    <xsl:call-template name="codeEntry">
     <xsl:with-param name="rule" select="."/>
     <xsl:with-param name="openBracket" select="following-sibling::node()[1]"/>
     <xsl:with-param name="emphTag" select="following-sibling::node()[2]"/>
     <xsl:with-param name="closeBracketString" select="following-sibling::node()[3]"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:copy-of select="."/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:for-each>

</xsl:template>


<xsl:template name="codeEntry">
<xsl:param name="rule"/>
<xsl:param name="openBracket"/>
<xsl:param name="emphTag"/>
<xsl:param name="closeBracketString"/>
<entry>
<xsl:copy-of select="$openBracket"/>
<xsl:copy-of select="$emphTag"/>
<xsl:text>] </xsl:text>
</entry>
<xsl:value-of select="substring-after($closeBracketString, ']')"/>

</xsl:template>

Obviously, the when statement grabs a group of nodes, but when each node goes through the otherwise block it gets copied to the result tree. I’m not really sure how to handle this since the para could have any number of these node groupings in any order, or none. (Once I figure this out I’ll add another when block that deals with the bracketed text before the rule)

I think creating a variable that tells the template to ignore the node is the way to go…but I’m a little foggy on the immutable variables and their scope…

I was also trying to think of a way I could try to do this recursively…but that would require adding a start tag at one point, an end tag in another, or no tag if the node being processed is in the middle of the sequence…and I know that can get weird in xslt.

Anyone run into this type of situation before?
thanks,
jason

any ideas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:17:07+00:00Added an answer on May 16, 2026 at 6:17 pm

    Just for fun (What a mess of a schema!), this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="node()|@*" name="identity">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="rule-line"/>
        <xsl:template match="emph">
            <entry>
                <xsl:text>[</xsl:text>
                <xsl:call-template name="identity"/>
                <xsl:text>]</xsl:text>
            </entry>
        </xsl:template>
        <xsl:template match="text()[normalize-space()='[']
                                   [following-sibling::*[1][self::emph]] |
                             text()[normalize-space()=']']
                                   [preceding-sibling::*[1][self::emph]]"
                      priority="1"/>
        <xsl:template match="text()[starts-with(normalize-space(),']')]
                                   [preceding-sibling::*[1][self::emph]]">
            <xsl:value-of select="substring-after(.,']')"/>
        </xsl:template>
        <xsl:template match="text()[substring(normalize-space(),
                                              string-length(normalize-space()),
                                              1) = '[']
                                   [following-sibling::*[1][self::emph]]">
            <xsl:call-template name="crop-both">
                <xsl:with-param name="pString" select="concat(']',.)"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template match="text()[starts-with(normalize-space(),']')]
                                   [substring(normalize-space(),
                                              string-length(normalize-space()),
                                              1) = '[']
                                   [preceding-sibling::*[1][self::emph]]
                                   [following-sibling::*[1][self::emph]]"
                      priority="1" name="crop-both">
            <xsl:param name="pString" select="."/>
            <xsl:variable name="vReverse">
                <xsl:call-template name="reverse">
                    <xsl:with-param name="pString" 
                                    select="substring-after(.,']')"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="reverse">
                <xsl:with-param name="pString"
                                select="substring-after($vReverse,'[')"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="reverse">
            <xsl:param name="pString"/>
            <xsl:if test="$pString!=''">
                <xsl:call-template name="reverse">
                    <xsl:with-param name="pString"
                                    select="substring($pString,2)"/>
                </xsl:call-template>
                <xsl:value-of select="substring($pString,1,1)"/>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <root>
    <para>Here is some text that will ask users to enter a <entry>[<emph type="it">date</emph>]</entry>, and maybe their <entry>[<emph type="it">name</emph>]</entry>. The text could come in different <entry>[<emph type="it">order</emph>]</entry>, and their could be any number of instances.</para>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.