Jon
Jon

Reputation: 263

XSL:IF within XSL:for-each-group

In my XSLT, I have something like:

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

<xsl:template match="PhyscianTotals" name="PhyscianTotals">
<xsl:for-each select="PhysicianTotals">
    <xsl:for-each-group select="Statistic" group-by="Type">
        <xsl:if test="Title='PHYSICIAN DETAIL TOTAL'">
            <xsl:element name="totals">
    </xsl:element>
     </xsl:if>
</xsl:for-each-group>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Is this valid XSLT? Specifically, the section of "xsl:if within the xsl:for-each-group". One of the XSLT compilation tool we call always error out stating: xsl:if is not allowed at this position in the stylesheet. If I remove the xsl:for-each-group, it passes. I am not sure if it's my xslt having errors or if it's the compilation tool.


Turns out our tool only support XSLT 1.0. So I guess I am back to rewrite the XSLT using 1.0 tags only.

The original XML looks like:

<?xml version="1.0" encoding="UTF-8"?>
<PhysicianTotals>
    <Statistic>
      <Title>PHYSICIAN TOTAL</Title>
      <Type>Type 1</Type>
      <Key>Cases</Key>
      <Value>1</Value>
    </Statistic>
    <Statistic>
      <Title>PHYSICIAN TOTAL</Title>
      <Type>Type 1</Type>
      <Key>Percentage</Key>
      <Value>25.0%</Value>
    </Statistic>
    <Statistic>
      <Title>PHYSICIAN TOTAL</Title>
      <Type>Type 2</Type>
      <Key>Cases</Key>
      <Value>3</Value>
    </Statistic>
    <Statistic>
      <Title>PHYSICIAN TOTAL</Title>
      <Type>Type 1</Type>
      <Key>Percentage</Key>
      <Value>75.0%</Value>
    </Statistic>
</PhysicianTotals>

And the output will look like:

<?xml version="1.0" encoding="UTF-8"?>
<totals>
<type>PHY_DETAIL</type>
<detailInfo>
    <code>Type 1</code>
</detailInfo>
<count>
    <caseValue>1</caseValue>
    <percentValue>25.0%</percentValue>
</count>
</totals>
<totals>
    <type>PHY_DETAIL</type>
<detailInfo>
    <code>Type 2</code>
</detailInfo>
<count>
    <caseValue>3</caseValue>
    <percentValue>75.0%</percentValue>
</count>
</totals>

Upvotes: 1

Views: 1455

Answers (2)

Michael Kay
Michael Kay

Reputation: 163458

Apart from the copy/paste error in the xsl:output declaration, your code looks perfectly OK to me. It's a bit suspect - do you really have an element called PhyscianTotals with a child called PhysicianTotals - so I suspect you're not showing us the code that actually generates the error.

Another possibility is that the tool generating the error is an XSLT 1.0 processor.

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

enchttp://stackoverflow.com/editing-helpoding="UTF-8"

This is syntactically/lexically illegal name: enchttp://stackoverflow.com/editing-helpoding

Did you mean encoding?

Apart from this there are no other lexical/syntax errors, and it seems that the transformation has a number of logical issues and is likely not to produce the wanted result -- but I cannot be 100% certain without seeing the source XML document and the desired result.

Upvotes: 0

Related Questions