简体   繁体   中英

XML Document Transform (XDT) locator for matching missing attribute

Say I have the following XML:

<logger>
    <level value="ALL" />
    <appender-ref ref="AsyncAppender" />
</logger>
<logger name="PerformanceMetricsLog">
    <level value="OFF" />
</logger>

I need to do an XML Transform on this.

How would I do a xdt:Locator to match or the first one? And on the second one?

I tried just doing xdt:Locator="Match(name)" and I got an error that there was not a "name" attribute. (I had hoped that if the attribute was not there it would just gracefully return false for the match.)

XDT's Match takes a comma-separated list of attribute names, so xdt:Locator="Match(name)" is expecting there to be an attribute with name, "name".

You may be thinking of XPath, where the absence of an attribute would simply return false, and you could test for its absence via not(@name) .

XDT has an XPath function, which takes an actual XPath, so you could write something like:

xdt:Locator="XPath(//logger[not(@name)])"

to select the nameless logger elements.

Note : Your XML as written is not well-formed. Be sure to have a single root element in your actual XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM