XML节点解析:移除空节点
Got it, let's fix that XML by stripping out those empty nodes. First, let's recap your input XML so we're on the same page:
<?xml version ='1.0'?> <cjischarges> <cjischarge> <cjisid>1</cjisid> <chargeid>1</chargeid> <cjiscode>10000</cjiscode> <class></class> <cjisclass>1 0000</cjisclass> <describe35>VESSEL-CONCEAL NAME/NUMBER</describe35> <describe70>VESSEL-CONCEAL NAME/NUMBER</describe70> <description>did operate and use a [boat/vessel] on the navigable waters Custom Service] was know...</description> </cjischarge> </cjischarges>
The empty node here is <class></class> — we want to remove any element that has no text content and no child elements. Below are a few practical ways to do this, depending on the language/tools you're using:
Method 1: XSLT (Universal XML Approach)
XSLT is a great language-agnostic option that works with any XML processor. Here's a stylesheet that will strip out all empty elements:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Copy all nodes by default --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- Skip copying elements that are completely empty --> <xsl:template match="*[not(node())]"/> </xsl:stylesheet>
You can run this with command-line tools like xsltproc or integrate it into code (most languages have XSLT libraries). The output will be your original XML minus the empty <class> node.
Method 2: Python (Using xml.etree.ElementTree)
If you're working in Python, the built-in ElementTree library makes this straightforward:
import xml.etree.ElementTree as ET # Parse the input XML tree = ET.parse("your_input.xml") root = tree.getroot() # Recursive function to remove empty elements def remove_empty_nodes(element): # Iterate backwards to avoid index issues when deleting for child in reversed(element): remove_empty_nodes(child) # Check if the child has no text and no children if not child.text and not child.getchildren(): element.remove(child) # Clean up the XML remove_empty_nodes(root) # Save the cleaned output tree.write("cleaned_output.xml", encoding="utf-8", xml_declaration=True)
This function traverses all nodes and removes any element that's completely empty.
Method 3: C# (Using LINQ to XML)
For .NET developers, LINQ to XML offers a concise way to handle this:
using System; using System.Xml.Linq; class Program { static void Main() { // Load the XML document XDocument doc = XDocument.Load("your_input.xml"); // Find and remove all empty elements doc.Descendants() .Where(e => string.IsNullOrWhiteSpace(e.Value) && !e.HasElements) .Remove(); // Save the cleaned XML doc.Save("cleaned_output.xml"); } }
The Descendants() method grabs all elements, then we filter for those with no value and no child elements, then remove them in one batch.
All these methods will give you the cleaned XML without the empty <class> node. Pick the one that fits your workflow best!
内容的提问来源于stack exchange,提问作者vb_lost

