D
D
darzet2011-04-21 11:46:07
XPath
darzet, 2011-04-21 11:46:07

How to make it so that empty nodes in xml are not displayed?

After converting to a certain pattern, nodes with empty entries sometimes appear. They need to not show up.
Probably somehow through the text() check. Something does not work.
Has anyone encountered such a problem?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
MikhailEdoshin, 2011-04-21
@MikhailEdoshin

If nodes are selected by XPath, you can only apply the pattern to nodes with text

/path/to/node[text()]

or, if it has subelements, and the entire value is taken
/path/to/node[string(.)]

or, if the pattern is applied during tree traversal, and not through selector for-each, then you can, respectively, specify in the pattern:
<xsl:template match="node[text()]">
   ...
</xsl:template>

or, if you create new nodes, and then apply-templates, and then it turns out that nothing came out and the element is empty as a result, then you can first apply-templatesinside the variable, and then check if anything worked out:
<xsl:variable name="contents">
  <xsl:apply-templates />
</xsl:variable>
<xsl:if test="string($contents)">
  <MyElement>
    <xsl:copy-of select="$contents" />
  </MyElement>
</xsl:variable>

Y
yadeveloper, 2011-04-21
@yadeveloper

Depends on your tree structure and template.
In general, here, read - soa-howto.blogspot.com/2009/09/how-to-remove-empty-nodes-from-xml.html

S
Sergey, 2011-04-21
@seriyPS

You can create a "delete template" for any tag that has no content. It will look something like this:

<xsl:template match="*[not text() and not self::*]"></xsl:template>

Let me tell you right now, I haven't tested it. Perhaps he will need to set the priority higher. patterns with * have low priority.

P
patashnik, 2011-04-21
@patashnik

So?

/root/node/child[text()]

D
darzet, 2011-04-21
@darzet

Thanks everybody.
I will have to rewrite the entire code.
I did everything without templates =(
Each node will have to be copy-pasted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question