0

I've the following xml,

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet xmlns="www.asdsad.com/sdsad">
  <Balances>
    <AccountNumber>KK-888</AccountNumber>
    <SubAccountNumber>KK-888-1</SubAccountNumber>
    <TAcctID>1</TAcctID>
    <TransactionAccount>ARC Deposit</TransactionAccount>
    <Description />
    <Balance>0.0000</Balance>
  </Balances>
  <Balances>
    <AccountNumber>KK-888</AccountNumber>
    <SubAccountNumber>KK-888-2</SubAccountNumber>
    <TAcctID>2</TAcctID>
    <TransactionAccount>Assessments and Dues</TransactionAccount>
    <Description>This is the primary account for all associations dues and assessments. </Description>
    <Balance>170</Balance>
  </Balances>
  <Balances>
    <AccountNumber>KK-888</AccountNumber>
    <SubAccountNumber>KK-888-4</SubAccountNumber>
    <TAcctID>4</TAcctID>
    <TransactionAccount>Fines/Compliance</TransactionAccount>
    <Description />
    <Balance>0.0000</Balance>
  </Balances>
</NewDataSet>

I need this result xml from above through xslt,

<balance amount="170" />

I'll pass the SubAccountNumber to the xslt and I need the particular Balances/Balance amount. In the above example I've passed the SubAccountNumber value as "KC1-0221-2" so the second Balances node is matched and it's Balance value is "170". Can anyone help me to write the xslt for that. (Note: only one node will match the passed SubAccountNumber).

UPDATED I've no problem if I can produce the xml with a root node,

<account>
     <balance amount="170" />
</account>
1
  • How will you pass input? using parameters? Commented Jun 8, 2011 at 11:11

1 Answer 1

1

A starting point...

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:asd="www.asdsad.com/sdsad"
    exclude-result-prefixes="asd">

    <xsl:output omit-xml-declaration="yes"/>
    <xsl:param name="SAN" select="'KC1-0221-2'"/>

    <xsl:template match="/asd:NewDataSet">
        <balance amount="{asd:Balances[asd:SubAccountNumber=$SAN]/asd:Balance}"/>
    </xsl:template>

</xsl:stylesheet>

Note that this will produce

<balance amount="" />

In the case $SAN is not present in the input document. Otherwise how you'd like to return in case of not matching at all?

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

2 Comments

The question also made mention of wanting the "first node", this answer doesn't address this part of the requirement.
@Michael-Kay: As usually happens, I think the question in the title does not really match the OP requ. However is just a matter of selecting the "first node", and we can easily tune the answer.

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.