0

I have an XML flow where I need to insert content based on the whether an element contains data or if it is empty.

I've tried several techniques but it's still not working.

Here is my XSLT:

<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="pricesample.xml" -->
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8"/>

<xsl:template match="catalog">

<catalog>
<xsl:for-each select="shoe">
<shoe>
<xsl:value-of select="name"/><xsl:text> </xsl:text>
<xsl:apply-templates select="price" />
</shoe>
</xsl:for-each>
</catalog>
</xsl:template>

<xsl:template match="price">
  <xsl:choose>
    <xsl:when test=". =''">
      <price><xsl:text>Price is Empty</xsl:text></price><xsl:text>
      </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <price><xsl:text> $</xsl:text><xsl:value-of select="."/></price><xsl:text>
      </xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


</xsl:stylesheet>

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<shoe>
    <name>Shoe 1</name>
    <price>49.98</price>
</shoe>
<shoe>
    <name>Shoe 2</name>
    <price>65.5</price>
</shoe>
<shoe>
    <name>Shoe 3</name>
    <price>70</price>
</shoe>
<shoe>
    <name>Shoe 4</name>
    <price/>
</shoe>
<shoe>
    <name>Shoe 5</name>
    <price/>
</shoe>
</catalog>

So the output should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<shoe>
    <name>Shoe 1</name>
    <price>$49.98</price>
</shoe>
<shoe>
    <name>Shoe 2</name>
    <price>$65.5</price>
</shoe>
<shoe>
    <name>Shoe 3</name>
    <price>$70</price>
</shoe>
<shoe>
    <name>Shoe 4</name>
    <price>Price is Empty</price>
</shoe>
<shoe>
    <name>Shoe 5</name>
    <price>Price is Empty</price>
</shoe>
</catalog>

I have tried several tests, including:

test=". =''"
test="not(price)"
test="not(string(.))"

None of them seem to work for me.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
Jim Maivald
  • 522
  • 6
  • 26
  • 1
    Before asking new questions, please take care of your last questions: [this one](http://stackoverflow.com/questions/27534532/grouping-data-using-xslt-is-not-working-completely) and [this one](http://stackoverflow.com/questions/27571400/how-can-you-add-two-decimal-places-to-numbers-that-dont-have-them). If there is a valid answer, accept it. If the question became obsolete, delete the question. – Mathias Müller Dec 19 '14 at 19:55
  • 1
    Oh, and also [this one](http://stackoverflow.com/questions/22018768/removing-line-breaks-and-broken-entities-using-xslt), [this one](http://stackoverflow.com/questions/22447495/picking-unique-heads) and [this one](http://stackoverflow.com/questions/22492073/is-there-an-easier-way-to-copy-image-elements) - and [this one](http://stackoverflow.com/questions/15356539/need-to-add-a-fourth-level-group-using-xslt). – Mathias Müller Dec 19 '14 at 19:58
  • 1
    And [this one](http://stackoverflow.com/questions/11357507/renaming-first-child-elements-only) - sorry for these spam-like comments, but you should really tidy up a dozen older questions before moving on. – Mathias Müller Dec 19 '14 at 20:04
  • If the question has not been answered I don't understand why I cannot ask another question. In previous questions I was told NOT to ask multiple questions in one post. Now you are telling me that if I have two questions I can't ask the second one until someone answers the first. Doesn't make much sense to me since some people have expertise in certain areas and not others. As for the answer here, I have not had a chance to test it yet. I will mark it solved when I do. (If it is.) Thanks – Jim Maivald Dec 19 '14 at 20:46
  • 1
    I see that you need close guidance on this. Accept your own answer to [this question](http://stackoverflow.com/questions/27534532/grouping-data-using-xslt-is-not-working-completely). Delete [this question](http://stackoverflow.com/questions/27571400/how-can-you-add-two-decimal-places-to-numbers-that-dont-have-them) - it is simply not true that there is a problem with the XLST code you wrote there. For [this question](http://stackoverflow.com/questions/22018768/removing-line-breaks-and-broken-entities-using-xslt) you promised to let the answerer "know if it works" - but did not do it. – Mathias Müller Dec 19 '14 at 20:51
  • 1
    You never really made it clear why the two answers to [this question](http://stackoverflow.com/questions/22447495/picking-unique-heads) are not acceptable. For [this question](http://stackoverflow.com/questions/15356539/need-to-add-a-fourth-level-group-using-xslt), a commenter made it clear that you did not clearly state the requirement and failed to show the expected output. For [this question](http://stackoverflow.com/questions/11357507/renaming-first-child-elements-only), there are several valuable answers, and you have not accepted any of them. – Mathias Müller Dec 19 '14 at 20:54

1 Answers1

2

Please do heed Mathias' suggestion to take care of your old questions. You should select an accepted answer when there is one that works, not just move on to the next question.

The answer to your question is fairly simple with an identity template and template pattern matching.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>

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

  <xsl:template match="price">
    <xsl:copy>
      <xsl:value-of select="concat('$', .)" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="price[not(node())]">
    <xsl:copy>Price is Empty</xsl:copy>
  </xsl:template>

</xsl:stylesheet>

When run on your sample input, the result is:

<catalog>
  <shoe>
    <name>Shoe 1</name>
    <price>$49.98</price>
  </shoe>
  <shoe>
    <name>Shoe 2</name>
    <price>$65.5</price>
  </shoe>
  <shoe>
    <name>Shoe 3</name>
    <price>$70</price>
  </shoe>
  <shoe>
    <name>Shoe 4</name>
    <price>Price is Empty</price>
  </shoe>
  <shoe>
    <name>Shoe 5</name>
    <price>Price is Empty</price>
  </shoe>
</catalog>

XsltCake

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • OK, but the original question also added a dollar sign to the output. Do I insert an `$` in front of `` – Jim Maivald Dec 19 '14 at 20:49
  • @JimMaivald Sorry, I missed that (please make sure to clearly describe the behavior you want and not just show an example of it). I've updated my answer. – JLRishe Dec 19 '14 at 20:55