在xslt中应用模板不起作用

时间:2014-08-19 07:25:13

标签: xml xslt

我是xsl转换的新手。我不知道为什么代码不会进入模板。我认为它的名称空间问题。

这是我的代码。 XSL代码:

<xsl:stylesheet version="1 .0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:conv="HSW.FileConvertor" exclude-result-prefixes="conv" xmlns:x="http://abcd.net/abcd/ws/incomingPayments/dto">
<xsl:output method="xml" encoding="utf-8" indent="no"/>

<xsl:template match="/">
    <xsl:value-of select="'&#10;PHONE;LANGUAGE;SMS_OPS_TYPE;CONTRACT_CODE;AMOUNT;DATE;DPD&#10;'"/>
    <xsl:apply-templates select="/x:incomingPayments/x:incomingPayment" />
</xsl:template>

<xsl:template match="x:incomingPayment">
    <xsl:if test="x:paymentChannel='DO_2013'">
        <xsl:variable name="Contract_code" select="x:contractNumber"/>
        <xsl:variable name="Amount" select="x:amount/x:moneyValue"/>
        <xsl:call-template name="Contract_search">
            <xsl:with-param name="Contract_code" select="$Contract_code"/>
            <xsl:with-param name="Amount" select="$Amount"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>
<xsl:template name="Contract_search">
    <xsl:param name="Contract_code"/>
    <xsl:param name="Amount"/>
    <xsl:value-of select="$Contract_code"/>
    <xsl:value-of select="'  '"/>
    <xsl:value-of select="$Amount"/>
    <xsl:value-of select="'&#10;'"/>
</xsl:template>

这是我的xml代码:

 <?xml version="1.0" encoding="UTF-8"?>
<hsIncomingPaymentsFile xmlns="http://abcd.net/abcd/ws/incomingPayments/dto">
<incomingPayments>
    <incomingPayment>
        <depositDate>2014-08-11</depositDate>
        <paymentChannel>DO_2013</paymentChannel>
        <contractNumber>3300867119</contractNumber><!-- extracting this tag -->
        <transactionDate>2014-08-11</transactionDate>
        <amount>
            <moneyValue>4850</moneyValue><!-- extracting this tag -->
            <currency>INR</currency>
        </amount>
        <transactionNumber/>
        <paymentType>PFC</paymentType>
    </incomingPayment>
<!-- second record -->
    <incomingPayment>
        <depositDate>2014-08-11</depositDate>
        <paymentChannel>DO_2013</paymentChannel>
        <contractNumber>3300861289</contractNumber><!-- extracting this tag -->
        <transactionDate>2014-08-11</transactionDate>
        <amount>
            <moneyValue>960</moneyValue><!-- extracting this tag -->
            <currency>INR</currency>
        </amount>
        <transactionNumber/>
        <paymentType>PFC</paymentType>
    </incomingPayment>
</incomingPayments>
</hsIncomingPaymentsFile >

1 个答案:

答案 0 :(得分:3)

问题在于这一行:

<xsl:apply-templates select="/x:incomingPayments/x:incomingPayment" />

您已使用正斜杠/开始表达式,该斜杠表示文档节点(它是XML中根元素的父级)。这意味着它期望根元素为x:incomingPayments,而不是它。您的根节点是x:hsIncomingPaymentsFile

请改为尝试:

<xsl:apply-templates select="x:hsIncomingPaymentsFile/x:incomingPayments/x:incomingPayment" />

试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:conv="HSW.FileConvertor" exclude-result-prefixes="conv" xmlns:x="http://abcd.net/abcd/ws/incomingPayments/dto">
<xsl:output method="text" encoding="utf-8" indent="no"/>

<xsl:template match="/">
    <xsl:value-of select="'&#10;PHONE;LANGUAGE;SMS_OPS_TYPE;CONTRACT_CODE;AMOUNT;DATE;DPD&#10;'"/>
    <xsl:apply-templates select="x:hsIncomingPaymentsFile/x:incomingPayments/x:incomingPayment" />
</xsl:template>

<xsl:template match="x:incomingPayment">
    <xsl:if test="x:paymentChannel='DO_2013'">
        <xsl:variable name="Contract_code" select="x:contractNumber"/>
        <xsl:variable name="Amount" select="x:amount/x:moneyValue"/>
        <xsl:call-template name="Contract_search">
            <xsl:with-param name="Contract_code" select="$Contract_code"/>
            <xsl:with-param name="Amount" select="$Amount"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template name="Contract_search">
    <xsl:param name="Contract_code"/>
    <xsl:param name="Amount"/>
    <xsl:value-of select="$Contract_code"/>
    <xsl:value-of select="'  '"/>
    <xsl:value-of select="$Amount"/>
    <xsl:value-of select="'&#10;'"/>
</xsl:template>
</xsl:stylesheet>

这会产生以下输出

PHONE;LANGUAGE;SMS_OPS_TYPE;CONTRACT_CODE;AMOUNT;DATE;DPD
3300867119  4850
3300861289  960

转到http://xsltransform.net/eiZQaES/1查看此操作