0

I am quite new to XSLT and find it very hard to understand properly..

I want to achieve that every element where my CDATA has no value should be removed from the XML.

My XML looks like this:

<description>
  <internet>
    <texts_short>
      <text locale="de"><![CDATA[]]></text>
      <text locale="en"><![CDATA[EXF/RETAIL]]></text>
    <texts_long>
      <text locale="de"><![CDATA[]]></text>
    </texts_long>
  </internet>
  <vehicle>
    <texts_short/>
    <texts_long>
      <text locale="en"><![CDATA[Backend Functions Bundle]]></text>
    </texts_long>
    <texts_text2speech>
      <text locale="de"><![CDATA[]]></text>
    </texts_text2speech>
  </vehicle>
</description>

I have already prepared the following XSLT which removes every text element with no condition:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text"/>

</xsl:stylesheet>

How can I achieve that only the elements with empty CDATA value will be removed - so that only the one with text "EXF/RETAIL" and "Backend Functions Bundle" will stay?

Thank you in advance!

2 Answers 2

1

What you can do is use <xsl:template match="text[not(normalize-space())]"/> but that will remove text elements having no content, an empty CDATA section, a CDATA section with only white space etc. There is no way in XSLT to know whether the element's content was provided in the lexical markup by a CDATA section or an ordinary text node.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like adding cdata-section-elements="text" to the output together with Martin Honnen's answer solved my problem:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" cdata-section-elements="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text[not(normalize-space())]"/>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.