Xslt if null




















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. First two deal with null value and second two deal with empty string. Aleksandar Borkovac Aleksandar Borkovac 3 3 silver badges 10 10 bronze badges. What if there are multiple users or multiple firstnames? Use xsl:apply-templates and matching templates to get what you want, much easier. This is probably the simplest XPath expression the one in accepted answer provides a test for the opposite, and would be longer, if negated : not string categoryName Explanation : The argument to the not function above is false exactly when there is no categoryName child "null" of the context item, or the single such categoryName child has string value -- the empty string.

Learn more about the importance of avoiding conditional constructs in this nice Pluralsight course: " Tactical Design Patterns in. Dimitre Novatchev Dimitre Novatchev k 26 26 gold badges silver badges bronze badges.

Hi Dimitre, i need your solution for 1. Please, post a question and send me a comment with a link. Follow the guidelines how to ask a good question. Hi Dimitre, I posted a question here stackoverflow. DustJones DustJones 71 1 1 silver badge 2 2 bronze badges. Dimitre Novatchev k 26 26 gold badges silver badges bronze badges.

Abel Abel Congrats on mentioning " Programming without if-branches ". There are some people who fail to understand the importance of this.

For all of them here is a link to a very nice Pluralsight course on this topic: " Tactical Design Patterns in. Marie Taylor Marie Taylor 41 1 1 bronze badge. The string value of an empty node set which is what the XPath expression categoryName gives you when there are no categoryName child elements in the current context is defined to be the empty string, so this is redundant - string-length categoryName is zero if there are no categoryName elements.

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.

This approach is more easily extendible then the imperative approach, because it automically deals with multiple categories and it can be expanded for other elements or exceptions by just adding another matching template. Programming without if-branches. Note: there is no such thing as null in XML. There is xsi:nil , but that is rarely used, especially rarely in untyped scenarios without a schema of some sort.

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 Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. I know it's an old question, but between all the answers, I miss a common approach to this use case in the development of XSLT.

That is, I assume that there can be zero, empty, single or multiple categoryName elements. Using the xsl:choose -style construct to handle all of these situations, or in other words, command wise, can quickly become confusing especially if the elements can be at different levels!

A typical programming habit in XSLT is to use templates and therefore T in XSLT , which is declarative rather than imperative you don'T tell the processor what to do, you tell you what you want to output as long as certain conditions are met.



0コメント

  • 1000 / 1000