xml - Test if nodes are empty or contain NULL using template match -



xml - Test if nodes are empty or contain NULL using template match -

edit xml

i trying figure out how test if nodes not exist or contain word null using template matching. using <xsl:choose> test in matched template. there more efficient way test?

in xml if or nodes not eixt, or contain word null want skip node , print out statement saying why node wrong (i.e. "the title not exist" or "the content null").

in xml have 3rd , 4th node should not display content , instead print statement saying why content not displayed.

here illustration of xml using test:

<?xml version="1.0" encoding="utf-8"?> <figures> <figure> <title>title 1</title> <desc>description 1</desc> <content>content 1</content> </figure> <figure> <title>title 2</title> <desc>description 2</desc> <content>content 2</content> </figure> <figure> <desc>description 3</desc> <content>content 3</content> </figure> <figure> <title>title 4</title> <desc>description 4</desc> <content>null</content> </figure> <figure> <title>title 5</title> <desc>description 5</desc> <content>content 5</content> </figure> </figures>

here xslt using:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" encoding="utf-8"/> <xsl:template match="/"> <div class="container"> <xsl:apply-templates /> </div> </xsl:template> <xsl:template match="figures"> <ul class="list"> <xsl:apply-templates select="figure" mode="titles" /> </ul> <div class="content"> <xsl:apply-templates select="figure" mode="content" /> </div> </xsl:template> <xsl:template match="figure" mode="titles"> <xsl:choose> <!-- check see if title field empty --> <xsl:when test="translate(./title,' ', '') = ''">the title node empty</xsl:when> <!-- check see if title field null --> <xsl:when test="normalize-space(./title) = 'null'">the title node null</xsl:when> <!-- check see if content field empty --> <xsl:when test="translate(./content,' ', '') = ''">the content node empty</xsl:when> <!-- check see if content field null --> <xsl:when test="normalize-space(./content) = 'null'">the content node null</xsl:when> <xsl:otherwise> <li> <a href="#section-{position()}"> <h3><xsl:value-of select="title" /></h3> </a> <p><xsl:value-of select="desc" /></p> </li> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="figure" mode="content"> <xsl:choose> <!-- check see if title field empty --> <xsl:when test="translate(./title,' ', '') = ''">the title node empty</xsl:when> <!-- check see if title field null --> <xsl:when test="normalize-space(./title) = 'null'">the title node null</xsl:when> <!-- check see if content field empty --> <xsl:when test="translate(./content,' ', '') = ''">the content node empty</xsl:when> <!-- check see if content field null --> <xsl:when test="normalize-space(./content) = 'null'">the content node null</xsl:when> <xsl:otherwise> <div id="section-{position()}"> <p><xsl:value-of select="content" /></p> </div> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

thank you.

this produce same result xslt , avoid utilize of xsl:choose. please give try:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" encoding="utf-8"/> <xsl:template match="/"> <div class="container"> <xsl:apply-templates /> </div> </xsl:template> <xsl:template match="figures"> <ul class="list"> <xsl:apply-templates select="figure" mode="titles" /> </ul> <div class="content"> <xsl:apply-templates select="figure" mode="content" /> </div> </xsl:template> <xsl:template match="figure" mode="titles"> <li> <a href="#section-{position()}"> <h3> <xsl:value-of select="title" /> </h3> </a> <p> <xsl:value-of select="desc" /> </p> </li> </xsl:template> <xsl:template match="figure[ *[self::title or self::content] [not(normalize-space(.)) or normalize-space(.) = 'null'] ]" mode="titles"> <xsl:apply-templates select="title | content" /> </xsl:template> <xsl:template match="figure" mode="content"> <div id="section-{position()}"> <p> <xsl:value-of select="content" /> </p> </div> </xsl:template> <xsl:template match="figure[ *[self::title or self::content] [not(normalize-space(.)) or normalize-space(.) = 'null'] ]" mode="content"> <xsl:apply-templates select="title | content" /> </xsl:template> <xsl:template match="title | content" /> <xsl:template match="*[self::title or self::content][not(normalize-space(.))]"> <xsl:value-of select="concat('the ', local-name(), ' node empty')"/> </xsl:template> <xsl:template match="*[self::title or self::content][normalize-space(.) = 'null']"> <xsl:value-of select="concat('the ', local-name(), ' node null')"/> </xsl:template> </xsl:stylesheet> please give try.

xml xslt

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -