0

I have XML files where i have to remove just 1 attribute.

After saving the edited XML the whitespaces are deleted, which i need! (see image)

When do i lose the whitespaces?

When i parse the xml in a doc? Or when i transform back to an xml?

I already use much Output Propertys from the Java Transformer like:

    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
    transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "RequestMessage Code");

to avoid other formating problems.

Is there a way to keep the whitespace?

Thank you

EDIT 1: XSL I use to avoid an alphabetical order

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" xalan:indent-amount="2"/>

    <!--Identity transformation (see http://www.w3.org/TR/xslt#copying).-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@Item"/>
            <xsl:apply-templates select="@Name"/>            
            <xsl:apply-templates select="@Include"/>
            <xsl:apply-templates select="@Variant"/>
            <xsl:apply-templates select="@Authorization"/>
            <xsl:apply-templates select="@Alias"/>
            <xsl:apply-templates select="@Source"/>
            <xsl:apply-templates select="@Field"/>
            <xsl:apply-templates select="@DgMemberName"/>
            <xsl:apply-templates select="@DgGroupName"/>
            <xsl:apply-templates select="@Target"/>
            <xsl:apply-templates select="@Host"/>
            <xsl:apply-templates select="@DataGroup"/>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Here u see the Diff between the two XML files

10
  • 2
    Why do you need bunch of spaces between attributes ? I doubt that the transformer has any option for such a strange thing . Commented Jun 14, 2016 at 9:03
  • Its a requirement. With the space it's easier to read.. Commented Jun 14, 2016 at 9:08
  • Please try to increase indent-amount to some higher value. Commented Jun 14, 2016 at 9:11
  • 2
    XML is there to store data, not to present data. If you need a cute presentation, consider throwing an XSL transformation in . Commented Jun 14, 2016 at 9:13
  • 1
    There are no XML parsers/generators what will retain spaces between attributes. Only way for that to happen, is for you to read and update the XML text yourself. Highly discouraged. Commented Jun 14, 2016 at 9:25

1 Answer 1

1

I think the answer to your question is that it can't be done using any readily-available XML serializer.

I sympathise with the requirement because I've got XML files formatted like that too. But I think it's a sufficiently rare use case that if you want to tackle it, you'll need to write your own serializer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.