I want to identify xml nodes that does not have specific attribute. Below is the sample xml. I want to retrieve nodes that does have the "response" attribute. I found atricles that help identify node that has a specific attribute but not my requirement.

<top node>
<viewentry position=1 children = 2>
</viewentry>
<viewentry position=1.1 children = 1 response=true>
</viewentry>
<viewentry position= 1.1.1 response=true>
</viewentry>
<viewentry position=2 children=1>
</viewentry>
<viewentry position=2.1 response=true>
</viewentry>
</top node>

Dani AI

Generated

was right to make the sample well-formed; attribute values must be quoted and element names cannot contain spaces. The approach used in the thread — using an XPath/XSLT predicate that tests for the absence of the attribute — is the simplest, most portable solution. The following notes add practical guidance and a couple of alternatives for common edge cases not shown above.

Common pitfalls and how to handle them

  • Distinguish "attribute absent" from "attribute present but empty" or "present with value 'false'": those are different conditions. If the attribute can be present but empty, test the string value (trim and check length) rather than only presence. If the attribute is used as a boolean flag, compare its string value explicitly (for example, test equality with "true") instead of relying on existence alone.
  • Namespaces: unprefixed element tests won’t match elements in a default namespace. Either register the namespace in the XPath engine or test the local-name() when a namespace prefix isn’t available.
  • DTD/XSD defaulting: an attribute provided by a DTD or schema may appear to be "present" even when not written in the source — account for schema defaults if the document is validated.

Practical alternative (LINQ-to-XML)

var missing = doc.Descendants("viewentry")
                 .Where(e => e.Attribute("response") == null);

foreach (var el in missing)
    Console.WriteLine((string)el.Attribute("position"));

This avoids XPath entirely and is often easier when working in .NET.

Quick troubleshooting checklist

  • Validate XML well-formedness.
  • Run the XPath/XSLT in a simple tester to confirm node-set matches expected elements.
  • If results are empty, check namespaces and whether the attribute is being supplied by a DTD/schema.

This complements the XSLT example already posted and addresses real-world variations that commonly break attribute-absence checks.

Recommended Answers

All 2 Replies

xml rules
so must xml

<?xml version="1.0"?>
<topnode>
    <viewentry position="1" children="2">
    </viewentry>
    <viewentry position="1.1" children="1" response="true">
    </viewentry>
    <viewentry position=" 1.1.1" response="true">
    </viewentry>
    <viewentry position="2" children="1">
    </viewentry>
    <viewentry position="2.1" response="true">
    </viewentry>
</topnode>

xpath is the way
attribute is addressed using @

to start i using xsl

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

    <xsl:template match="/">
        <root>
            <xsl:apply-templates select="topnode/viewentry"/>
        </root>
    </xsl:template>
    <xsl:template match="viewentry">
        <data>has @response</data>
    </xsl:template>
    <xsl:template match="viewentry[not(@response)]">
        <not></not>
    </xsl:template>
</xsl:stylesheet>

result

<?xml version='1.0' ?>
<root>
  <not/>
  <data>has @response</data>
  <data>has @response</data>
  <not/>
  <data>has @response</data>
</root>

That is exactly what I was looking for. Thanks much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.