XSLT应用于具有xmlns属性的XML文档 [英] XSLT applied to XML doc with xmlns attribute

查看:136
本文介绍了XSLT应用于具有xmlns属性的XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm applying an XSLT stylesheet to the following XML file:

<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文件(执行简单排序):

Here's my current XSLT file (performs a simple sort):

<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:'名称空间前缀.

Problem is, I do not know exactly how to use the 'doc:' namespace prefix with the xsl:template and xsl:apply-templates tags.

现在,XML文档按原样复制,因此我相信第一个xsl:template块已被应用.但是,这些项目没有排序,因此我认为问题出在第二个xsl:template.

Right now, the XML document is copied as-is, so I believe the first xsl:template block is being applied. However, the items are unsorted, so I think the problem lies in the second xsl:template.

我应该注意,如果我从两个文件中都删除了xmlns属性,则转换将正常进行.

I should note that if I remove the xmlns attributes from both files, the transformation works properly.

有什么建议吗?

(问题基于此示例 )

推荐答案

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

Have you tried prefixing element names with the doc: namespace prefix in your select attributes?

<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>

这篇关于XSLT应用于具有xmlns属性的XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆