user1747980
user1747980

Reputation: 255

How to convert XML empty tag

This might sound strange but I need to convert all empty tags to a pair of open and closing tag with a carrage return in between. Thanks in advance.

From

<OBR>
<OBR_31_Lab_Ins_Typ_Id>L</OBR_31_Lab_Ins_Typ_Id>
<OBX>
<OBX_3_Ltt_Cd>N877</OBX_3_Ltt_Cd>
<OBX_5_1_Lbr_No>0</OBX_5_1_Lbr_No>
<OBX_5_2_Lbr_Tx/>
<OBX_6_Lbr_Unt_Tx/>
</OBX>
<OBX>
<OBX_3_Ltt_Cd>N878</OBX_3_Ltt_Cd>
<OBX_5_1_Lbr_No>-15</OBX_5_1_Lbr_No>
<OBX_5_2_Lbr_Tx/>
<OBX_6_Lbr_Unt_Tx/>
</OBX>
<OBR>

To

<OBR>
<OBR_31_Lab_Ins_Typ_Id>L</OBR_31_Lab_Ins_Typ_Id>
<OBX>
<OBX_3_Ltt_Cd>N877</OBX_3_Ltt_Cd>
<OBX_5_1_Lbr_No>0</OBX_5_1_Lbr_No>
<OBX_5_2_Lbr_Tx>

</OBX_5_2_Lbr_Tx>
<OBX_6_Lbr_Unt_Tx>

</OBX_6_Lbr_Unt_Tx>
</OBX>
<OBX>
<OBX_3_Ltt_Cd>N878</OBX_3_Ltt_Cd>
<OBX_5_1_Lbr_No>-15</OBX_5_1_Lbr_No>
<OBX_5_2_Lbr_Tx>

</OBX_5_2_Lbr_Tx>
<OBX_6_Lbr_Unt_Tx>

</OBX_6_Lbr_Unt_Tx>
</OBX>
<OBR>

Upvotes: 3

Views: 168

Answers (2)

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56162

Use:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="*[not(node())]">
    <xsl:copy>
      <xsl:text>&#13;&#10;</xsl:text>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 3

adrusi
adrusi

Reputation: 845

you could use a regular expression like this:

s/\<([a-zA-z0-9-_]+(\s+[a-zA-z0-9-_]+=".*?")*?\s*\/\>/<$1>\n\n<\/$1>/g

Upvotes: 0

Related Questions