首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT (XML到XML)输出包含来自源的不匹配数据。

XSLT (XML到XML)输出包含来自源的不匹配数据。
EN

Stack Overflow用户
提问于 2012-04-11 10:15:38
回答 2查看 1.7K关注 0票数 2

使用Visual在开发期间执行转换,生成的xml包含目标中的源xml中的文本,这些文本包含在与我的模板条件不匹配的标记中

我希望第一个模板中的select组能够找到任何名为Group的元素,这些元素是CrystalReport的直接子元素,并在应用模板调用中传递它们。我了解到,我的第二个模板上的匹配筛选器将只接受具有Level=1属性的Group,并将它们写出来。我希望其他的都会被忽略。

为什么“这个”不出现在我的输出中?

来源

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<!--
 UPDATE: Note that adding the xmlns attribute causes all output
 to disappear unless you use Chris's second solution.
 -->
<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail" >
  <Group Level="1">
    <GroupHeader>
      <Section>
        <Field FieldName="apple" />
      </Section>
    </GroupHeader>
    <Group Level="2">
      not this
    </Group>
  </Group>
</CrystalReport>

转换

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/CrystalReport">
    <root>
        <xsl:apply-templates select="Group"/>
    </root>
  </xsl:template>

  <xsl:template match="Group[@Level='1']/GroupHeader">
    <tag1><xsl:value-of select="Section/Field/@FieldName"/></tag1>
  </xsl:template>
</xsl:stylesheet>

输出

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<root>
  <tag1>apple</tag1>
      not this
    </root>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-11 10:25:51

您所面临的问题是XML解析器的内置模板正在发挥作用。您将模板应用于所有Group元素,但只使用自己的模板捕捉其中的一个。另一个由默认模板处理,该模板输出所有节点的值。我建议你改变一下

代码语言:javascript
复制
 <xsl:template match="/CrystalReport">

转到

代码语言:javascript
复制
 <xsl:template match="/">

这将覆盖生成额外输出的根默认模板。您可以在http://www.w3.org/TR/xslt#built-in-rule上找到关于内置模板规则的更多信息。

然后重写基本text()模板,以便最终XSLT看起来有点像这样

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
    <root>
        <xsl:apply-templates select="CrystalReport/Group"/>
    </root>
</xsl:template>
<xsl:template match="Group[@Level='1']/GroupHeader">
    <tag1>
        <xsl:value-of select="Section/Field/@FieldName"/>
    </tag1>
</xsl:template>
<xsl:template match="text()"/>

更新

或者更简单地说,您只需匹配所需的元素并使用如下所示

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/CrystalReport">
    <root>
        <xsl:apply-templates select="Group[@Level='1']/GroupHeader"/>
    </root>
</xsl:template>
<xsl:template match="GroupHeader">
    <tag1>
        <xsl:value-of select="Section/Field/@FieldName"/>
    </tag1>
</xsl:template>

这将使默认的text()模板就位,这非常方便。

票数 2
EN

Stack Overflow用户

发布于 2012-04-11 11:27:58

试一试

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:crystal="urn:crystal-reports:schemas:report-detail">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/crystal:CrystalReport">
    <root>
        <xsl:apply-templates select="crystal:Group[@Level='1']/crystal:GroupHeader"/>
    </root>
</xsl:template>
<xsl:template match="crystal:GroupHeader">
    <tag1>
        <xsl:value-of select="crystal:Section/crystal:Field/@FieldName"/>
    </tag1>
</xsl:template>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10103979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档