开发者

Question reg. XSLT transform

I've a below xml,

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet xmlns="www.reefpt.com/caliberapi">
  <ARTransactions>
    <PostingDate>2010-01-01T00:00:00-07:00</PostingDate>
    <Description>Quarterley Assessment</Description>
    <Amount>47</Amount>
  </ARTransactions>
  <ARTransactions>
    <PostingDate>2010-01-03T00:00:00-07:00</PostingDate>
 开发者_开发百科   <Description>Payment, Thank You.</Description>
    <Amount>-43</Amount>
  </ARTransactions>
  <ARTransactions>
    <PostingDate>2010-04-15T00:00:00-07:00</PostingDate>
    <Description>Quarterley Assessment</Description>
    <Amount>23</Amount>
  </ARTransactions>
</NewDataSet>

I want to transform it into,

<trxs>
    <trx trx_credit="47" trx_debit="0.00" />
    <trx trx_credit="0.00" trx_debit="43" />
    <trx trx_credit="23" trx_debit="0.00" />    
<trxs>

for each ARTransactions element if it has a positive Amount then it should come in trx_credit else it should come int trx_debit. So every generated trx element will contain either a credit or debit and the other will be 0.00. How can I write a XSLT for this? Can anybody help me?


Here's a simpler version of the 20-line second template in Treemonkey's solution (requires XSLT 2.0)

<xsl:template match="ARTransactions">
  <trx trx_credit="{(Amount[. > 0], 0.00)[1]}"
       trx_debit="{(abs(Amount[. > 0], 0.00)[1])}"/>
</xsl:template>

XSLT is often criticized for being verbose, but it doesn't have to be.


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="NewDataSet">
            <trxs>
                    <xsl:apply-templates select="ARTransactions"/>
            </trxs>
    </xsl:template>
        <xsl:template match="ARTransactions">
                <xsl:element name="trx">
                  <xsl:attribute name="trx_credit">    
                  <xsl:choose>
                    <xsl:when test="Amount &gt; 0" >
                      <xsl:value-of select="Amount"/>
                    </xsl:when>
                    <xsl:otherwise>0.00</xsl:otherwise>
                  </xsl:choose>
                  </xsl:attribute>
                  <xsl:attribute name="trx_debit">
                  <xsl:choose>
                    <xsl:when test="Amount &gt; 0">0.00</xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="substring-after(Amount,'-')"/>
                    </xsl:otherwise>
                  </xsl:choose>
                  </xsl:attribute>                        
                </xsl:element>
        </xsl:template>
</xsl:stylesheet>


You can do this by using the xsl attribute element, to conditionally add attributes. The snippet below will create a trx element for every ARTransactions.

<xsl:for-each select="//ARTransactions">
  <trx>
    <xsl:choose>
      <xsl:when test="Amount >= 0">
        <xsl:attribute name="trx_credit" select="Amount" />
        <xsl:attribute name="trx_debit" select="0" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="trx_debit" select="-1 * Amount" />
        <xsl:attribute name="trx_credit" select="0" />
      </xsl:otherwise>
    </xsl:choose>
  </trx>
</xsl:for-each>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜