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

  • Home
  • SEARCH
  • 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 5935959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:19:48+00:00 2026-05-22T15:19:48+00:00

I am attempting to perform some text canonicalization to replace some contractions. Here is

  • 0

I am attempting to perform some text canonicalization to replace some contractions. Here is some example input:

<?xml version="1.0"?>
<transcript>
  <p id="p1">
    <s id="s1"><w>Here</w><w>'s</w> <w>an</w> <w>example</w>, <w>let</w><w>'s</w> <w>consider</w> <w>it</w></s>
    <s id="s2"><w>Here</w> <w>'s</w> <w>an</w> <w>example</w>, <w>let</w><w>'s</w> <w>consider</w> <w>it</w></s>
    <s id="s3"><foo><w>Here</w></foo><bar><w>'s</w></bar> <w>an</w> <w>example</w>, <foo><w>let</w></foo><w>'s</w> <w>consider</w> <w>it</w></s>
    <s id="s4"><w>Here</w><bar><baz><w>'s</w></baz></bar> <w>an</w> <w>example</w>, <baz><bar><w>let</w></bar><w>'s</w></baz> <w>consider</w> <w>it</w></s>
    <s id="s5"><w>Look</w> <w>here</w></s>
    <s id="s6"><w>'s</w> <w>another</w> <w>example</w></s>
  </p>
</transcript>

In this example, I want to replace “here’s” with “hers is” and “let’s” with “let us”. Thus, my desired output is,

<?xml version="1.0"?>
<transcript>
  <p id="p1">
    <s id="s1"><w>Here</w> <w>is</w> <w>an</w> <w>example</w>, <w>let</w> <w>us</w> <w>consider</w> <w>it</w></s>
    <s id="s2"><w>Here</w>  <w>is</w> <w>an</w> <w>example</w>, <w>let</w>  <w>us</w> <w>consider</w> <w>it</w></s>
    <s id="s3"><foo><w>Here</w></foo> <bar><w>is</w></bar> <w>an</w> <w>example</w>, <foo><w>let</w></foo> <w>us</w> <w>consider</w> <w>it</w></s>
    <s id="s4"><w>Here</w> <bar><baz><w>is</w></baz></bar> <w>an</w> <w>example</w>, <baz><bar><w>let</w></bar> <w>us</w></baz> <w>consider</w> <w>it</w></s>
    <s id="s5"><w>Look</w> <w>here</w></s>
    <s id="s6"><w>'s</w> <w>another</w> <w>example</w></s>
  </p>
</transcript>

I was able to put together some (probably nothing near elegant or optimal) code that can handle s1 and s2, but I do not see that I can generalize it to something useful.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="w[translate(text(),'S','s')=&quot;'s&quot;][preceding-sibling::*[1]/self::w[translate(text(),'HERE','here')='here']]">
  <xsl:text> </xsl:text>
  <xsl:copy><xsl:copy-of select="@*"/>is</xsl:copy>
</xsl:template>

<xsl:template match="w[translate(text(),'S','s')=&quot;'s&quot;][preceding-sibling::*[1]/self::w[translate(text(),'LET','let')='let']]">
  <xsl:text> </xsl:text>
  <xsl:copy><xsl:copy-of select="@*"/>us</xsl:copy>
</xsl:template>

</xsl:stylesheet>

Some details:

  • Assume words are all wrapped in <w> tags and that the “words” of interest are consecutive (though not necessarily siblings)

  • Arbitrary tags may wrap one or the other or both of the word and the ‘s.

  • The substitution should not cross sentence <s> boundaries (as shown in s5 and s6) – though if this is impossible, I will not cry.

  • If a space already exists between word and ‘s, I still want to replace the ‘s. The exact spacing of the result (one space or two) does not matter.

  • Ideally, the space will be added to the nearest common ancestor of the two <w> tags containing the word and the ‘s.

Thanks for any guidance you can give!

  • 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-22T15:19:48+00:00Added an answer on May 22, 2026 at 3:19 pm

    This transformation fulfills all the requirements:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:my" exclude-result-prefixes="my">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
     <my:AposS>'s</my:AposS>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "w[. = document('')/*/my:AposS
       and
         not(generate-id()
            =
             generate-id(ancestor::s[1]/descendant::w[1])
             )
       and
         preceding::w[1] = 'Here'
        ]
      ">
      <w>is</w>
     </xsl:template>
    
     <xsl:template match=
      "w[. = document('')/*/my:AposS
       and
         not(generate-id()
            =
             generate-id(ancestor::s[1]/descendant::w[1])
             )
       and
         preceding::w[1] = 'let'
        ]
      ">
      <w>us</w>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <transcript>
        <p id="p1">
            <s id="s1">
                <w>Here</w>
                <w>'s</w>
                <w>an</w>
                <w>example</w>, 
                <w>let</w>
                <w>'s</w>
                <w>consider</w>
                <w>it</w>
            </s>
            <s id="s2">
                <w>Here</w>
                <w>'s</w>
                <w>an</w>
                <w>example</w>, 
                <w>let</w>
                <w>'s</w>
                <w>consider</w>
                <w>it</w>
            </s>
            <s id="s3">
                <foo>
                    <w>Here</w>
                </foo>
                <bar>
                    <w>'s</w>
                </bar>
                <w>an</w>
                <w>example</w>, 
                <foo>
                    <w>let</w>
                </foo>
                <w>'s</w>
                <w>consider</w>
                <w>it</w>
            </s>
            <s id="s4">
                <w>Here</w>
                <bar>
                    <baz>
                        <w>'s</w>
                    </baz>
                </bar>
                <w>an</w>
                <w>example</w>, 
                <baz>
                    <bar>
                        <w>let</w>
                    </bar>
                    <w>'s</w>
                </baz>
                <w>consider</w>
                <w>it</w>
            </s>
            <s id="s5">
                <w>Look</w>
                <w>here</w>
            </s>
            <s id="s6">
                <w>'s</w>
                <w>another</w>
                <w>example</w>
            </s>
        </p>
    </transcript>
    

    the wanted result is produced:

    <transcript>
       <p id="p1">
          <s id="s1">
             <w>Here</w>
             <w>is</w>
             <w>an</w>
             <w>example</w>, 
                <w>let</w>
             <w>us</w>
             <w>consider</w>
             <w>it</w>
          </s>
          <s id="s2">
             <w>Here</w>
             <w>is</w>
             <w>an</w>
             <w>example</w>, 
                <w>let</w>
             <w>us</w>
             <w>consider</w>
             <w>it</w>
          </s>
          <s id="s3">
             <foo>
                <w>Here</w>
             </foo>
             <bar>
                <w>is</w>
             </bar>
             <w>an</w>
             <w>example</w>, 
                <foo>
                <w>let</w>
             </foo>
             <w>us</w>
             <w>consider</w>
             <w>it</w>
          </s>
          <s id="s4">
             <w>Here</w>
             <bar>
                <baz>
                   <w>is</w>
                </baz>
             </bar>
             <w>an</w>
             <w>example</w>, 
                <baz>
                <bar>
                   <w>let</w>
                </bar>
                <w>us</w>
             </baz>
             <w>consider</w>
             <w>it</w>
          </s>
          <s id="s5">
             <w>Look</w>
             <w>here</w>
          </s>
          <s id="s6">
             <w>'s</w>
             <w>another</w>
             <w>example</w>
          </s>
       </p>
    </transcript>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im attempting to perform a basic query against some XML in an XML typed
I'm attempting to implement a feature where a user can select some text in
I'm attempting to create a Spring application (NOT web application) to perform some simple
I'm working with selenium to perform some automation and I'm attempting to interact with
I'm attempting to perform a DML operation against an Entity Framework model, specifically, an
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to perform a synchronous write/read in a demux-based client application with MINA
I'm attempting to perform dynamic sorting of data that I'm putting into grids into
I am attempting to perform the LoadProperty operation from my context to load a
I'm attempting to perform a link of previously generated .obj files (using the latest

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.