开发者

Selecting the first xml node using xslt

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>


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜