使用XSL删除额外的值

时间:2016-07-28 10:49:29

标签: xml xslt

如何删除转换后生成的额外值。

输入:

<response status="200">
    <IDList>
        <IDLists>
            <IDSec>
                <IDField>
                    <IDFields>
                        <ID>123456</ID>
                        <IDState>done</IDState>
                    </IDFields>
                    <IDFields>
                        <ID>12345634</ID>
                        <IDState>failed</IDState>
                    </IDFields>
                </IDField>
            </IDSec>
        </IDLists>
        <code>56</code>
        <msg>yet</msg>
    </IDList>
</response>

XSL用于转换它:

<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:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:if test="/response/IDList/IDLists/IDSec/IDField/IDFields/ID">
            <xsl:apply-templates/>
        </xsl:if>
    </xsl:template>
    <xsl:template match="IDFields">
        <cid>
            <id>
                <xsl:value-of select="ID"/>
            </id>
            <iDState>
                <xsl:value-of select="IDState"/>
            </iDState>  
        </cid>
    </xsl:template>
</xsl:stylesheet>

必填项:

<cid>
  <id>123456</id>
  <iDState>done</iDState>
</cid>
<cid>
  <id>12345634</id>
  <iDState>failed</iDState>
</cid>

使用上述XSL后生成的输出:

<cid>
  <id>123456</id>
  <iDState>done</iDState>
</cid>
<cid>
  <id>12345634</id>
  <iDState>failed</iDState>
</cid>56yet

如何从输出中删除额外的 56yet

1 个答案:

答案 0 :(得分:1)

更改

<xsl:template match="/">
    <xsl:if test="/response/IDList/IDLists/IDSec/IDField/IDFields/ID">
        <xsl:apply-templates/>
    </xsl:if>
</xsl:template>

<xsl:template match="/">
        <xsl:apply-templates select="/response/IDList/IDLists/IDSec/IDField/IDFields[ID]"/>
</xsl:template>