SMA_JAVA
SMA_JAVA

Reputation: 511

Getting Xml Value based on two attribute conditions

My Xml Looks like:

      <Taxes>
      <MyTax currency="USD" MyTaxCat="City">0.00000</MyTax>
      <MyTax currency="USD" MyTaxCat="County">0.01000</MyTax>
      <MyTax currency="USD" MyTaxCat="District">0.00000</MyTax>
      <MyTax currency="USD" MyTaxCat="State">0.01000</MyTax>
      </Taxes>

My XSLT :

                <MyTaxes>
            <xsl:for-each select="Taxes/MyTax"> 
                        <Tax>
                        <xsl:attribute name="TaxCategory">VAT</xsl:attribute>
                        <xsl:attribute name="TaxName">
                        <xsl:value-of select="@MyTaxCat" /> 
                        </xsl:attribute>
                        <xsl:attribute name="TaxPerLine">
                        <xsl:value-of select="/MyTax[@currency='USD']"/>
                        </xsl:attribute>
                        </Tax>

                        </xsl:for-each>
        </MyTaxes>

In my output, all TaxPerLine is getting set as 0.0000

      <MyTaxes>
          <Tax TaxCategory="VAT" TaxName="City" TaxPerLine="0.00000" />
          <Tax TaxCategory="VAT" TaxName="County" TaxPerLine="0.00000" />
          <Tax TaxCategory="VAT" TaxName="District" TaxPerLine="0.00000" />
          <Tax TaxCategory="VAT" TaxName="State" TaxPerLine="0.00000" />
         </MyTaxes>

Any idea what am I doing wrong here?

Thanks!!

Upvotes: 0

Views: 117

Answers (2)

Sojimanatsu
Sojimanatsu

Reputation: 601

look the part of /MyTax[@currency='USD']"/> you have already defined the path MyTax in the beginning so anyway the program will ignore your request.

Either you can define another each-for loop for the part or you can try by just usung current()[@currency='USD'] Besides,you shouldnt start with "/" Ex:/MyTax because you dont have any root element defined before MyTax.

That gave me the right output.Good Luck.

Upvotes: 1

Ajeet Singh
Ajeet Singh

Reputation: 1076

Check this code I think your output get

    <xsl:decimal-format name="ajeet" decimal-separator="." grouping-separator=","/>
<xsl:template match="/root">
<MyTaxes>
<xsl:for-each select="Taxes/MyTax"> 
<Tax>
<xsl:attribute name="TaxCategory">VAT</xsl:attribute>
<xsl:attribute name="TaxName">
<xsl:value-of select="@MyTaxCat" /> 
</xsl:attribute>
<xsl:attribute name="TaxPerLine">
<xsl:value-of select="format-number(current()[@currency='USD'], '0.0000', 'ajeet')"/>
</xsl:attribute>
</Tax>
</xsl:for-each>
</MyTaxes>

Upvotes: 1

Related Questions