xslt输出不需要的文本 - 不是默认模板

时间:2014-07-14 10:48:43

标签: xml xslt-1.0

我检查了各种关于不需要的文字的帖子,但这似乎不是那个问题,所以我希望这个名单上的大师可以帮助我。如果我错过了一些非常愚蠢的事情,请提前道歉。我确实尝试添加“空”'用于解决默认文本问题的模板。

这是我的XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <EstimateDisplayRequest>
      <facXML>
        <faRecord>
         <recordCode>33</recordCode>
         <item>
            <itemMapCode>1</itemMapCode>
            <itemValue>A</itemValue>
         </item>
         <item>
            <itemMapCode>110</itemMapCode>
            <itemValue>*</itemValue>
         </item>
         <item>
            <itemMapCode>111</itemMapCode>
            <itemValue>C</itemValue>
         </item>
         <item>
            <itemMapCode>112</itemMapCode>
            <itemValue>20140528</itemValue>
         </item>
         <item>
            <itemMapCode>113</itemMapCode>
            <itemValue>154245</itemValue>
         </item>
         <item>
            <itemMapCode>114</itemMapCode>
            <itemValue>MCSTST</itemValue>
         </item>
         <item>
            <itemMapCode>115</itemMapCode>
            <itemValue>TRACYF</itemValue>
         </item>
         <item>
            <itemMapCode>116</itemMapCode>
            <itemValue>Tracy</itemValue>
         </item>
         <item>
            <itemMapCode>117</itemMapCode>
            <itemValue>Fry</itemValue>
         </item>
         </faRecord>
      </facXML>
   </EstimateDisplayRequest>

这是我尝试使用的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="EstimateDisplayRequest">
        <MCSResponse>
            <EstimateDisplayResponse>
                <xsl:apply-templates select="*" mode="EstimateDisplayResponse" />
            </EstimateDisplayResponse>
        </MCSResponse>
    </xsl:template>

    <xsl:template match="/">
    <xsl:apply-templates/>
    </xsl:template>

     <xsl:template match="text()"/>

    <xsl:template match="faRecord/item[itemMapCode='1' and itemValue='A']" mode="EstimateDisplayResponse">
        <EstimateDisplayResponseDetailAudit>
            <xsl:apply-templates select="following-sibling::*" mode="EstimateDisplayResponseDetailAudit" />
        </EstimateDisplayResponseDetailAudit>
    </xsl:template>  


    <xsl:template match="item[itemMapCode='110']" mode="EstimateDisplayResponseDetailAudit" >
        <FromStatus>
            <xsl:value-of select="./itemValue" />
        </FromStatus>
    </xsl:template>
    <xsl:template match="item[itemMapCode='111']" mode="EstimateDisplayResponseDetailAudit" >
        <ToStatus>
            <xsl:value-of select="./itemValue" />
        </ToStatus>
    </xsl:template>
    <xsl:template match="item[itemMapCode='112']" mode="EstimateDisplayResponseDetailAudit" >
        <ClientContactDateAudit>
            <xsl:value-of select="./itemValue" />
        </ClientContactDateAudit>
    </xsl:template>
    <xsl:template match="item[itemMapCode='113']" mode="EstimateDisplayResponseDetailAudit" >
        <Time>
            <xsl:value-of select="./itemValue" />
        </Time>
    </xsl:template>
    <xsl:template match="item[itemMapCode='114']" mode="EstimateDisplayResponseDetailAudit" >
        <UserId>
            <xsl:value-of select="./itemValue" />
        </UserId>
    </xsl:template>
    <xsl:template match="item[itemMapCode='115']" mode="EstimateDisplayResponseDetailAudit" >
        <PersonCode>
            <xsl:value-of select="./itemValue" />
        </PersonCode>
    </xsl:template>
    <xsl:template match="item[itemMapCode='116']" mode="EstimateDisplayResponseDetailAudit" >
        <PersonFirstName>
            <xsl:value-of select="./itemValue" />
        </PersonFirstName>
    </xsl:template>
    <xsl:template match="item[itemMapCode='117']" mode="EstimateDisplayResponseDetailAudit" >
        <PersonLastName>
            <xsl:value-of select="./itemValue" />
        </PersonLastName>
    </xsl:template>
    <xsl:template match="item[itemMapCode='118']" mode="EstimateDisplayResponseDetailAudit" >
        <Comment>
            <xsl:value-of select="./itemValue" />
        </Comment>
    </xsl:template>
    <xsl:template match="item[itemMapCode='119']" mode="EstimateDisplayResponseDetailAudit" >
        <AuditGAPStatus>
            <xsl:value-of select="./itemValue" />
        </AuditGAPStatus>
    </xsl:template>
    <xsl:template match="item[itemMapCode='120']" mode="EstimateDisplayResponseDetailAudit" >
        <AuditGAPExpiryDate>
            <xsl:value-of select="./itemValue" />
        </AuditGAPExpiryDate>
    </xsl:template>
    <xsl:template match="item[itemMapCode='121']" mode="EstimateDisplayResponseDetailAudit" >
        <AuditGAPEmailAddressUsed>
            <xsl:value-of select="./itemValue" />
        </AuditGAPEmailAddressUsed>
    </xsl:template>
    <xsl:template match="item[itemMapCode='122']" mode="EstimateDisplayResponseDetailAudit" >
        <AuditGAPComments>
            <xsl:value-of select="./itemValue" />
        </AuditGAPComments>
    </xsl:template>
</xsl:stylesheet>

但这是我得到的输出:

    <mcsresponse>
      <estimatedisplayresponse>
        33
        <estimatedisplayresponsedetailaudit>
            <fromstatus>*</fromstatus>
            <tostatus>C</tostatus>
            <clientcontactdateaudit>20140528</clientcontactdateaudit>
            <time>154245</time>
            <userid>MCSTST</userid>
            <personcode>TRACYF</personcode>
            <personfirstname>Tracy</personfirstname>
            <personlastname>Fry</personlastname>
        </estimatedisplayresponsedetailaudit>
        110
        *
        111
        C
        112
        20140528
        113
        154245
        114
        MCSTST
        115
        TRACYF
        116
        Tracy
        117
        Fry
    </estimatedisplayresponse>
</mcsresponse>

有人能指出我哪里错了吗? TIA。

1 个答案:

答案 0 :(得分:1)

文本节点未按预期被禁止的原因是:

<xsl:template match="EstimateDisplayRequest">
    <MCSResponse>
        <EstimateDisplayResponse>
            <xsl:apply-templates select="*" mode="EstimateDisplayResponse" />
        </EstimateDisplayResponse>
    </MCSResponse>
</xsl:template>

但你没有:

<xsl:template match="text()" mode="EstimateDisplayResponse" />

-
附:这不是更简单吗?例如,有些东西:

<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:template match="/">
   <MCSResponse>
        <EstimateDisplayResponse>
            <xsl:for-each select="EstimateDisplayRequest/facXML/faRecord">
                <xsl:value-of select="recordCode" />
                <FromStatus>
                    <xsl:value-of select="item[itemMapCode='110']/itemValue" />
                </FromStatus>
                <ToStatus>
                    <xsl:value-of select="item[itemMapCode='111']/itemValue" />
                </ToStatus>
                <!-- ... -->
            </xsl:for-each>
        </EstimateDisplayResponse>
    </MCSResponse>
</xsl:template>

</xsl:stylesheet>