【问题标题】:no output of xslt transformation cause of used namespace没有输出 xslt 转换原因使用的命名空间
【发布时间】:2023-04-02 12:29:01
【问题描述】:

我有一个像下面这样的xml

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"
  xmlns:c="http://base.google.com/cns/1.0">
  <entry>
    <g:id>1207</g:id>
    <g:price>25.90 EUR</g:price>
  </entry>
 </feed>

我想使用以下 xsl 创建数据的 csv 输出

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"
    xmlns:c="http://base.google.com/cns/1.0">
    <xsl:output method="text" indent="yes"/>
    <xsl:template match="/">id;price       
        <xsl:for-each select="feed/entry">
            <xsl:value-of select="g:id"/>;<xsl:text>&#xA;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我需要以下输出,但只打印出标题

id;title       
1207;25.90 EUR

我想命名空间的某些东西是丢失数据的原因,有什么想法吗?

【问题讨论】:

  • 是的,Stack Overflow 上有数百个问题正好解决了这个问题,请尝试搜索“xslt 默认命名空间”之类的内容。
  • @Ian,这就是我在这里问的原因,我还没有找到对我真正有帮助的东西。我花了很多时间寻找解决方案,但最后我卡住了。

标签: xml xslt csv namespaces


【解决方案1】:

我想命名空间的某些东西是 缺失数据

是的,没错。您的样式表忽略了 &lt;feed&gt;&lt;entry&gt; 拥有自己的命名空间这一事实。您在样式表中错误地将此命名空间声明为默认命名空间。试试吧:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://www.w3.org/2005/Atom" 
xmlns:g="http://base.google.com/ns/1.0">

<xsl:output method="text"/>

<xsl:template match="/">id;price       
    <xsl:for-each select="a:feed/a:entry">
        <xsl:value-of select="g:id"/>;<xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

编辑:

当然,要获得所需形式的输出,模板需要看起来更像:

<xsl:template match="/">
    <xsl:text>id;price&#xA;</xsl:text>
    <xsl:for-each select="a:feed/a:entry">
        <xsl:value-of select="g:id"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="g:price"/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
</xsl:template>

【讨论】:

  • 感谢迈克尔的帮助,因为你的例子。我现在更清楚地看到在 xml 转换中使用命名空间
  • @happymapper 如果这回答了您的问题,请通过接受答案来关闭它。
猜你喜欢
  • 2010-12-16
  • 1970-01-01
  • 2018-06-27
  • 2018-01-20
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多