6

我将 XSLT 样式表应用于以下 XML 文件:

<top xmlns="http://www.foo.com/bar">
    <elementA />
    <elementB />
    <contents>
        <contentitem>
                <id>3</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
        <contentitem>
                <id>2</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
        <contentitem>
                <id>1</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
    </contents>
</top>

这是我当前的 XSLT 文件(执行简单排序):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:doc="http://www.foo.com/bar">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--                                                -->
 <xsl:strip-space elements="*"/>
<!--                                                -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                                -->
  <xsl:template match="contents">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="contentitem">
        <xsl:sort select="id" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

问题是,我不知道如何使用带有 xsl:template 和 xsl:apply-templates 标签的 'doc:' 命名空间前缀。

现在,XML 文档是按原样复制的,所以我相信第一个 xsl:template 块正在应用。但是,这些项目是未排序的,所以我认为问题出在第二个 xsl:template 上。

我应该注意,如果我从两个文件中删除 xmlns 属性,则转换工作正常。

有什么建议么?

(问题基于这个例子

4

1 回答 1

11

您是否尝试过doc:在选择属性中使用命名空间前缀为元素名称添加前缀?

<xsl:template match="doc:contents">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="doc:contentitem">
      <xsl:sort select="doc:id" data-type="number"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
于 2009-09-09T18:34:14.497 回答