【问题标题】:How to assign namespace in xsl:template如何在 xsl:template 中分配命名空间
【发布时间】:2016-01-07 11:45:46
【问题描述】:

对于关于 xsl:template 和命名空间的第一百个问题,我深表歉意。我确实阅读了其他问题(例如 hereherehere),但仍然无法将这些问题拼凑在一起。

这是我的输入:

<?xml version="1.0" encoding="UTF-8" ?>
<AuthorIT version="6.2.0" xmlns="http://www.authorit.com/xml/authorit"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
    <Objects>
        <Book>
            <Object>
                <Description>1089-802-01</Description>
            </Object>
        </Book>
    </Objects>
</AuthorIT>

这是我的样式表:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ait="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.authorit.com/xml/authorit AuthorIT.xsd">
<xsl:template match="/">
    <Table>
        <apply-templates/>
    </Table>
</xsl:template>
<xsl:template match="ait:Book">
    <Row>
        <Cell>
            <Data><xsl:value-of select="./Object/Description"/></Data>
        </Cell>
    </Row>
</xsl:template>

预期结果:

<Table>
    <Row>
        <Cell>
            <Data>1089-802-01</Data>
        </Cell>
    </Row>
</Table>

实际结果:

<Table>
       1089-802-01
</Table>

换句话说,xsl:template match="/" 匹配,但 xsl:template match="ait:Book" 不匹配。

如何匹配 Book 元素?

请注意,我从输入根元素 AuthorIT xmlns="http://www.authorit.com/xml/authorit" 中获取了默认命名空间,并将其作为 添加到样式表中xmlns:ait="http://www.authorit.com/xml/authorit"。 然后我尝试将 Book 元素匹配为 ait:Book。但我一定误会了this answer

我不知道它是否重要,但我正在使用 Saxon HE 9 和 Notepad++ XML 插件。

【问题讨论】:

    标签: xml xslt namespaces


    【解决方案1】:

    &lt;apply-templates/&gt; 是一个文字结果元素,你需要&lt;xsl:apply-templates/&gt;。但是我没有得到你说你得到的结果(http://xsltransform.net/ej9EGcB),我得到了

    <?xml version="1.0" encoding="UTF-8"?><Table xmlns:ait="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><apply-templates/></Table>
    

    这清楚地表明您只需创建一个带有apply-templates 子元素的Table 元素。

    【讨论】:

      【解决方案2】:

      正如 Martin Honnen 已经指出的那样,使用您的代码收到的实际结果并不是您声称的那样。

      无论如何,要得到预期的结果,你必须改变

      <xsl:value-of select="./Object/Description"/> 
      

      到:

      <xsl:value-of select="./ait:Object/ait:Description"/>
      

      因为这些元素也在给定的命名空间中。

      您还应该添加exclude-result-prefixes="ait" 作为xsl:stylesheet 元素的属性,并删除那里的冗余命名空间声明。

      这是对&lt;apply-templates/&gt;&lt;xsl:apply-templates/&gt; 问题的补充。

      当然你需要一个结束 &lt;/xsl:stylesheet&gt; 标记。

      【讨论】:

        【解决方案3】:

        谢谢大家的回答。

        我做了以下事情:

        • 向样式表标签添加了xmlns:ait="http://www.authorit.com/xml/authorit" 声明。
        • 在相关的地方添加了ait: 前缀。
        • 在样式表标签中添加了exclude-result-prefixes="ait"

        它现在可以工作了,虽然我不明白exclude-result-prefixes="ait" 做了什么:无论有没有它,转换都很顺利。也许是未来研究的主题。

        &lt;apply-templates/&gt;&lt;xsl:apply-templates/&gt; 问题和结束 &lt;/xsl:stylesheet&gt; 标记是在简化帖子文件时出现的拼写错误。对此感到抱歉,感谢您指出。

        特别感谢 Martin Honnen 提供指向 http://xsltransform.net 的链接:我不知道它存在,它非常方便。

        【讨论】:

          【解决方案4】:

          尝试使用

          <xsl:apply-templates/>
          

          (您缺少“xsl:”) 和

          <xsl:template match="AuthorIT/Object/Books">
          

          而不是“ait:Book”。 这应该可以解决问题。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-04-06
            • 1970-01-01
            • 1970-01-01
            • 2019-02-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-03
            相关资源
            最近更新 更多