Xsl equals null




















Learn more about clone URLs. Download ZIP. XSLT: Check if a string is null or empty. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters. Copy link. Now I've put a condition to the second and is only running in case the first one failed Thanks again.

Great, Thanks a lot. Continue this way Asked 12 years, 8 months ago. Active 9 months ago. Viewed k times. Improve this question. Isaac G Sivaa 1, 4 4 gold badges 14 14 silver badges 32 32 bronze badges. Can you expand the code example? Depending on your use-case, you probably don't want to use xsl:when for node-tests. The processor will then make the correct decisions for you and you do not need to write out the business logic in nested xsl:choose anymore.

In many cases, using matching templates makes writing stylesheets easier. Add a comment. Active Oldest Votes. Improve this answer. The detailed semantics of this test is: return true if there is at least one categoryName element whose string value is an empty string.

I'm an xsl newbie, so forgive any potential stupidity to my question. This answer, while accepted and highly voted, is also very misleading. It really depends what you mean by "null or empty". The supplied answer will return false if the categoryName element is absent, which in my interpretation of the question makes it a wrong answer. MichaelKay: I've updated the answer to provide more details. Show 5 more comments.

Dirk Vollmar k 53 53 gold badges silver badges bronze badges. It's appreciated that you included multiple examples to show how each expression results. Instead, you only deal with nodes, or elements in this case, which is the whole between start-tag and end-tag. I starred the question specifically for this answer, and while the question is pretty old, this one seems much more deserving of being the selected answer — Patrick.

From Empty Element : To test if the value of a certain node is empty It depends on what you mean by empty. Contains no child nodes: not node Contains no text content: not string. Contains no text other than whitespace: not normalize-space. Contains nothing except comments: not node [not self::comment ]. Mads Hansen Chris Doggett Chris Doggett Some notes. The first bulletpoint also tests for text-content, which is also a node. The second bulletpoint tests for any text node at any depth, if you want to know if the current node does not contain text, but can contain other nodes, you can use not text.

An alternative to your 2nd bullet is also not. As your last bullet shows: there are many ways to consider "nothingness" ;. Very practical: To test if a string is not empty, you can just test the string itself!

What about? This works great. In some cases, you might want to know when the value is specifically null, which is particularly necessary when using XML which has been serialized from. NET objects. While the accepted answer works for this, it also returns the same result when the string is blank or empty, i.

Sometimes it's necessary to know the exact state and you can't simply check if CategoryName is instantiated, because unlike say Javascript. If there is a possibility that the element does not exist in the XML I would test both that the element is present and that the string-length is greater than zero:.

I know this question is old, but between all the answers, I miss one that is a common approach for this use-case in XSLT development. To deal with all these cases using xsl:choose -style constructs, or in other words, imperatively, is quickly getting messy even more so if elements can be at different levels!

A typical programming idiom in XSLT is using templates hence the T in XSLT , which is declarative programming, not imperative you don't tell the processor what to do, you just tell what you want output if certain conditions are met. For this use-case, that can look something like the following:. This works with any XSLT version , because the first one above has a higher precedence it has a predicate. The "fall-through" matching template, the second one, catches anything that is not valid.

The third one then takes care of outputting the categoryName value in a proper way. Note that in this scenario there is no need to specifially match categories or category , because the processor will automatically process all children, unless we tell it otherwise in this example, the second and third template do not further process the children, because there is no xsl:apply-templates in them.



0コメント

  • 1000 / 1000