?
Solved

XML Formatting

Posted on 2008-10-09
11
Medium Priority
?
405 Views
Last Modified: 2013-11-18
Is there any way to add line breaks to an XML file after each element using XSLT or C#?

The problem is I have multiple files (several hundred thousand) that were all exported into XML as a single line. For the input program to accept them, they have to include line breaks to be more properly formatted. Any ideas on how to accomplish this?


Before
 
<document xmlns="http://www.ademero.com/XmlSchemas/ContentCentral/XmlCaptureDescriptorV1.1" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd" xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd"><electronicFile>Images\AIRCO-W8371_1.pdf</electronicFile><destination><catalog documentType="default">Accounts Payable</catalog></destination><fields><field name="Invoice">W8371</field><field name="Vendor">AIRCO INC</field><field name="Date">12/18/98</field></fields></document>
 
 
AFTER
 
<document xmlns="http://www.ademero.com/XmlSchemas/ContentCentral/XmlCaptureDescriptorV1.1" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd" xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd">
  <electronicFile>Images\AIRCO-W8371_1.pdf</electronicFile>
  <destination>
    <catalog documentType="default">Accounts Payable</catalog>
  </destination>
  <fields>
    <field name="Invoice">W8371</field>
    <field name="Vendor">AIRCO INC</field>
    <field name="Date">12/18/98</field>
  </fields>
</document>

Open in new window

0
Comment
Question by:developernetwork
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 18

Expert Comment

by:zc2
ID: 22678775
Try to use an identity transformation, it should output with line breaks, especially if you use <xsl:output> with the indent="yes" attribute
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" encoding="iso-8859-1"/>
 
	<xsl:template match="@*|node()">
	  <xsl:copy>
	    <xsl:apply-templates select="@*|node()"/>
	  </xsl:copy>
	</xsl:template>	
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:developernetwork
ID: 22678925
I'm a little confused.

Here, let me show you the XSLT stylesheet we're already using.
I have the indent="yes" in there and it fails to do that.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" media-type="text/xml" omit-xml-declaration="no" version="1.0"/>
	<xsl:template match="/" xmlns:addData="http://www.abbyy.com/FlexiCapture/Schemas/Export/AdditionalFormData.xsd"
	xmlns:form="http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd">
		<document xmlns="http://www.ademero.com/XmlSchemas/ContentCentral/XmlCaptureDescriptorV1.1">
			<electronicFile>
				<xsl:value-of select="form:Documents/*/@addData:ImagePath" />
			</electronicFile>
			<destination>
				<catalog documentType="default">Accounts Payable</catalog>
			</destination>
			<fields>
				<field name="Invoice">
					<xsl:value-of select="form:Documents/*/_Document/_Invoice" />
				</field>
				<field name="Vendor">
					<xsl:value-of select="form:Documents/*/_Document/_Vendor" />
				</field>
				<field name="Date">
					<xsl:value-of select="form:Documents/*/_Document/_Date" />
				</field>
			</fields>
		</document>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 18

Expert Comment

by:zc2
ID: 22680013
May I ask you what the xsl processor are you using?
I tried on MSXML v3 and v6, it does obey the indent attribute.
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

Author Comment

by:developernetwork
ID: 22681459
I'm using a simple Xsl.Transform command in Microsoft C# version 3.5
0
 

Author Comment

by:developernetwork
ID: 22681524
I'm not familiar with how to use MSXML v6, I've just written a little program in C# that does the transform.
0
 

Author Comment

by:developernetwork
ID: 22681723
Did you make changes to the XSLT that I posted to make it do that? I'm trying again and I'm just not able to get it to format the document. I'm extremely new to working with XML, and they've put me on a very short deadline, so I haven't had the chance to read a lot of what I would have if I had been granted more time on this project.
0
 
LVL 18

Expert Comment

by:zc2
ID: 22683800
You don't need a complex xslt to just reformat the xml with the same structure it already has.
All you need is a copy xslt which I have provided you in the first comment.

I tried to use the xsl from a C# program, and you right it does ignore the indent="yes" attribute.

Try to use the copy xsl with the following command line utility, it uses MSXML v3 as far as I know.
http://www.microsoft.com/downloads/details.aspx?FamilyID=2fb55371-c94e-4373-b0e9-db4816552e41&DisplayLang=en
0
 

Author Comment

by:developernetwork
ID: 22684123
Well, this isn't the original document. The XSLT transforms it from a different document.

The reason I use the C# tool is because I have to execute this on a couple hundred thousand documents.
0
 
LVL 10

Assisted Solution

by:Hans Langer
Hans Langer earned 800 total points
ID: 22684191
Just load the XML on a XML object and then save it or call his outerxml property. Both method ident the data.

xml.xmlDocument oXML = new xml.xmldocument;
oXML.loadxml(sXML)


oXML.outterXML

or

oXML.save("c:\myfile.xml")
0
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 1200 total points
ID: 22687638
This might help...
xmlNode = the xml data
 
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.Indentation = 4;
xmlWriter.IndentChar = ' ';
xmlWriter.QuoteChar = '"';
 
xmlNode.WriteTo(xmlWriter);
 
return stringWriter.ToString();

Open in new window

0
 

Author Comment

by:developernetwork
ID: 22688594
Thanks, using all of this I was able to design a working solution. I appreciate it!
0

Featured Post

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question