开发者

Java Style XML one node per line and no whitespace

开发者 https://www.devze.com 2023-03-22 17:55 出处:网络
Just fooling with removing whitespace but keeping each node on its own line from an xml document when adding and removing elements from xml in java and I\'m having trouble understanding XML Style Shee

Just fooling with removing whitespace but keeping each node on its own line from an xml document when adding and removing elements from xml in java and I'm having trouble understanding XML Style Sheets.

Here is what's happening so far.

Firstly I have the following XML,

<jobs>
   <job>Job 1</job>
   <job>Job 2</job>
   <job>Job 3</job>
   <job>Job 4</job>
</jobs>

Then I remove one of the elements and it ends up looking like this with the whitespacewhere the element was,

<jobs>
   <job>Job 1</job>

   <job>Job 3</job>
   <job>Job 4</job>
</jobs>

So I tried applying the following style sheet I found,

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output met开发者_高级运维hod="xml" omit-xml-declaration="yes"/>

  <xsl:strip-space elements="*"/>

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

</xsl:stylesheet>

Which makes the xml appear on one line because it removes all whitespace. But I'm trying to keep the file readable too.

<jobs><job>Job 1</job><job>Job 2</job><job>Job 3</job><job>Job 4</job></jobs>

I was wondering if anyone has a style sheet to achieve this?


You need to add indent="yes" to <xsl:output:

<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

(Also, you might want to switch to XSL Version 2.0)

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消