Home > Blockchain >  how through xslt variable value select node
how through xslt variable value select node

Time:09-13

I am new to the xslt.

 - **Here is my input xml.**

<root>
        <MilleniumCreditCard>
            <Limit>20</Limit>
            <overdraft>yes</overdraft>
        </MilleniumCreditCard>
        <VisaCreditCard>
            <Limit>30</Limit>
            <overdraft>yes</overdraft>
        </VisaCreditCard>
        <OtherTypeCreditCard>
            <Limit>40</Limit>
            <overdraft>yes</overdraft>
        </OtherTypeCreditCard>
    </root>

As we can see we have 3 types of credit card.

But at a time only one type of card can come in the input xml. 
So the actual input xml would for example.

<root>
    <MilleniumCreditCard>
        <Limit>20</Limit>
        <overdraft>yes</overdraft>
    </MilleniumCreditCard>
</root>

I need to fetch the Limit and overdraft value for the same. but as I don't know which card will be coming , I have do a logic in my common template as shown below.

Hence I have tried to write a common template

1st I am calling the template like below

<output>
    <Limit>
    <xsl:call-template name="commontemplate">
        <xsl:with-param name="inputname" select ="'Limit'"
    </xsl:call-template>
    </Limit>
  <overdraft>
    <xsl:call-template name="commontemplate">
        <xsl:with-param name="inputname" select ="'overdraft'"
    </xsl:call-template>
    </overdraft>
    </output>

Now my common template as below

 <xsl:template name="commontemplate">
        <xsl:param name="inputname" />
        <xsl:choose>
            <xsl:when test="root/MilleniumCreditCard != '' " />
            <xsl:value-of select="root/MilleniumCreditCard/$inputname" />
        </xsl:when>
        <xsl:when test="root/VisaCreditCard != '' " />
        <xsl:value-of select="root/VisaCreditCard/$inputname" />
    </xsl:when>
    <xsl:when test="root/OtherTypeCreditCard != '' " />
    <xsl:value-of select="root/OtherTypeCreditCard/$inputname" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="''">
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

but I am , getting blank value for Limit tag in output. but I want to get the value like below .

<output>
<Limit>20</Limit>
<overdraft>yes</overdraft>
</output>

Can anybody help me with how to achieve this? I will greatly appreciate for this help

CodePudding user response:

This is rather confusing. If the input will always have details of a single card only, you could do simply:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/root">
    <output>
        <xsl:copy-of select="*/*"/>
    </output>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

Two XSLT/XPath features you need to learn about:

(a) the wildcard * selects any element regardless of its name, for example /root/* in your example.

(b) the union expression (A|B|C) selects an element whose name is A B, or C. In XSLT 2.0 you can use this on the right-hand-side of /, for example root/(A|B|C); in XSLT 1.0 you would have to write this as (root/A | root/B | root/C).

  • Related