XSLT获得了一些不需要的价值

时间:2011-01-29 17:13:02

标签: xml xslt

XML:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE UploadXML SYSTEM "ex_v222.dtd">
    <UploadXML><Version>1.1</Version>
<Properties>
<Property>
<IntegratorID>3232321</IntegratorID>
<IntegratorPropertyID>12312312-3</IntegratorPropertyID>
<IntegratorOfficeID>1231231231</IntegratorOfficeID>....

由于某种原因,以下XSL文件始终获取1.1值并将其放在信封元素之前:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:param name="Z">2312</xsl:param>
    <xsl:param name="A">KKK</xsl:param>
    <xsl:output method="xml" encoding="utf-8" indent="yes" />
        <xsl:strip-space elements="*" />

        <xsl:template match='/UploadXML/Properties'> 
        <Envelope>
        <Body>
        <add_adverts>
        <xsl:apply-templates select='Property'/>
        </add_adverts>
        </Body>
        </Envelope>
        </xsl:template>

....(我相信模板实现并不重要..)

我得到的是:

<?xml version="1.0" encoding="utf-8"?>
1.1<Envelope xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <Body>
    <add_adverts>
      <advert>

。 。

看到1.1那里?为什么? 任何想法?

2 个答案:

答案 0 :(得分:6)

您看到了这一点,因为这是XSLT对于与任何模板都不匹配的节点的默认行为。要明确不复制Version元素内容,您只需使用空模板:

<xsl:template match="Version"/>

答案 1 :(得分:2)

您需要阅读有关XSLT处理模型的信息,并了解如何使用 XSLT built-in (default) templates

观察到的行为是由于文本节点的XSLT内置模板,它只是复制它:

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

解决方法是为任何不应出现在输出中的文本节点覆盖此模板,其模板具有空主体(不执行任何操作,因此不会复制文本节点)。在这种情况下:

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