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

Strip XML code to just leave text

Status
Not open for further replies.

wajwo

Programmer
Mar 4, 2005
3
GB
Hi everyone

I have XML files that I want to strip down to just be Text, which basically means getting rid of all the code in pointy brackets.

So
<Created>24-Feb-05 03:26:57 PM</Created>

becomes just 24-Feb-05 03:26:57 PM

This is so that I can give non-programmers a Word version of the file that is relatively easy to read, without all the clever bits.

Is there a program that does this, or can I write a simple macro in Word that deletes everything inside pointy brackets?

Sorry this isn't a very technical question!

Thanks in advance
CW
 
Well, you could just use string replace with wildcards and use \<*\> but that wouldn't utilising the full power of XML. Best would be to use a stylesheet and give them HTML view:

Make the top of your XML this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
Then save the following as test.xsl in the same folder as the XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>My XML file</title>
      </head>
      <body>
        <h2>Created: <xsl:value-of select="//Created"/></h2>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Now open the XML in internet explorer. Alternatively, to output all the text, use this stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>My XML file</title>
      </head>
      <body>
        <p>All of my XML: <xsl:value-of select="*"/></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
There's no end to what you can do with XSL.

Jon
 
Thanks Jon

I will have a bash at this

XML / XSL is all a bit new to me to be honest!
 
Hi Jon

I followed your steps, made the xsl file in the same folder and added the 2 lines of code to the xml file, but just got the following message in IE

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'file:///I:/Methods/XML to Word Doc Macro/Room101.xml'....

<?xml-stylesheet type="text/xsl" href="test.xsl"?>



I did delete a line:
<?xml version="1.0" encoding="utf-8" ?>

because you said to insert line
<?xml version="1.0" encoding="ISO-8859-1"?>

Perhaps this was the wrong thing to do...

Maybe it is just because I am using WordPad - top quality!
 
OK, try:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

Also make sure wordpad doesnt add any formatting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top