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 6055885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:18:52+00:00 2026-05-23T08:18:52+00:00

Seems like this should be easy, but … I’m trying to use XSLT to

  • 0

Seems like this should be easy, but …

I’m trying to use XSLT to extract part of an XML file as plain text, throwing away the rest.

So from sample input like this …

<?xml version="1.0" encoding="UTF-8"?>
<?oxygen RNGSchema="http://segonku.unl.edu/teianalytics/TEIAnalytics.rng"
                        type="xml"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0" n="Wright2-0034.sgml.xml">
   <teiHeader type="text">
      <fileDesc>
         <titleStmt>
            <title>Header Title</title>
         </titleStmt>
         <publicationStmt>
            <p>Published</p>
         </publicationStmt>
         <sourceDesc>
            <p>Sourced</p>
         </sourceDesc>
      </fileDesc>
   </teiHeader>
   <text>
      <front>
      </front>
      <body>
         <head>THE TITLE</head>
         <div type="chapter" part="N" org="uniform" sample="complete">
            <head>CHAPTER I</head>
            <p>Some text.</p>
         </div>
      </body>
   </text>
</TEI>

… I’m trying to get just the text contained within the <body> tags and all their children. The desired output in this case is:

THE TITLE
CHAPTER I
Some text.

Potential complication: <body> can also exist in the <front> matter and/or in the <teiHeader>, so what I really need is the children of <body> if and only if that tag is a child of <text> and of <TEI>.

I’ve tried really simple XSL like this …

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="/TEI/text/body">
        <xsl:apply-templates select="."/>
    </xsl:template>
</xsl:stylesheet>

… but it gives me plain text of everything in the file, not just the <body> elements.

Thanks!

  • 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-23T08:18:52+00:00Added an answer on May 23, 2026 at 8:18 am

    I’ve tried really simple XSL like this
    …

    …

         <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
             <xsl:output method="text"/>
             <xsl:template match="/TEI/text/body">
                 <xsl:apply-templates select="."/>
             </xsl:template>
         </xsl:stylesheet>
    

    … but it gives me plain text of
    everything in the file, not just the
    <body> elements.

    The reason for this is a famous property/feature of XPath (and reason for many thousands similar questions) to consider any unprefixed name as belonging to “no namespace. However, any element in the provided XML document belongs to the namespace: “http://www.tei-c.org/ns/1.0” and must be accessed as a node in this namespace.

    Solution: Define the documents default namespace in the XSLT code (this time with a prefix bound to it) and use the prefix in specifying every name.

    This is one of the simplest and shortest possible transformations that produces the wanted result:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:x="http://www.tei-c.org/ns/1.0">
     <xsl:output method="text"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="x:text/x:body//text()">
      <xsl:value-of select="concat(.,'&#xA;')"/>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    When applied on the provided XML document:

    <TEI xmlns="http://www.tei-c.org/ns/1.0" n="Wright2-0034.sgml.xml">
        <teiHeader type="text">
            <fileDesc>
                <titleStmt>
                    <title>Header Title</title>
                </titleStmt>
                <publicationStmt>
                    <p>Published</p>
                </publicationStmt>
                <sourceDesc>
                    <p>Sourced</p>
                </sourceDesc>
            </fileDesc>
        </teiHeader>
        <text>
            <front>      </front>
            <body>
                <head>THE TITLE</head>
                <div type="chapter" part="N" org="uniform" sample="complete">
                    <head>CHAPTER I</head>
                    <p>Some text.</p>
                </div>
            </body>
        </text>
    </TEI>
    

    the wanted, correct result is produced:

    THE TITLE
    CHAPTER I
    Some text.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like this should be the easy part, but I can't seem to get
This seems like it should be really easy but I couln't find an answer
This seems like it should be easy, but I can't quite find an explanation
This seems like it should be an easy thing to do, but for the
This seems like it should be something very easy to do, but every time
Seems like in Perl this should be easy or a module, but I have
Seems like this should be easy, but I'm not finding a simple configuration setting.
This seems like it should be easy as pie, but I can't seem to
This seems like it should be an easy fix, but after searching for hours
This seems like it should be prettty easy - but I just can't get

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.