Xml 匹配元素忽略命名空间

Xml 匹配元素忽略命名空间,xml,xslt,Xml,Xslt,我从一个服务中获得了以下xml: <GetClass_RS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Class xmlns="http://www.company.com/erp/worker/soa/2013/03"> <student>Jack</student>

我从一个服务中获得了以下xml:

<GetClass_RS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Class xmlns="http://www.company.com/erp/worker/soa/2013/03">
    <student>Jack</student>
    <student>Harry</student>
    <student>Rebecca</student>
    <teacher>Mr. Bean</teacher>
  </Class>
</GetClass_RS>

杰克
骚扰
丽贝卡
憨豆先生
我想这样匹配班级/学生:

<xsl:template match="Class/student">
    <p>
        <xsl:value-of select="."/>
    </p>
</xsl:template>
<Class xmlns="http://www.company.com/erp/worker/soa/2013/03">


问题是由于类的命名空间匹配不起作用:

<Class xmlns="http://www.company.com/erp/worker/soa/2013/03">

我想忽略匹配中的名称空间


好的,我想我不需要忽视它。我只需要匹配一下 具有名称空间的元素

首先,请注意,如果名称空间是这样声明的:

<xsl:template match="Class/student">
    <p>
        <xsl:value-of select="."/>
    </p>
</xsl:template>
<Class xmlns="http://www.company.com/erp/worker/soa/2013/03">
声明此命名空间并定义前缀。为元素添加前缀就像是声明其名称空间的简写方式

样式表

<?xml version="1.0" encoding="UTF-8"?>
<p xmlns:class="http://www.company.com/erp/worker/soa/2013/03">Jack</p>
<p xmlns:class="http://www.company.com/erp/worker/soa/2013/03">Harry</p>
<p xmlns:class="http://www.company.com/erp/worker/soa/2013/03">Rebecca</p>
我添加了一个与
teacher
元素匹配的模板。否则,它们的文本内容将通过XSLT处理器的默认行为输出

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:class="http://www.company.com/erp/worker/soa/2013/03">

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

   <xsl:template match="/">
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="class:Class/class:student">
    <p>
        <xsl:value-of select="."/>
    </p>
   </xsl:template>

   <xsl:template match="class:teacher"/>

</xsl:stylesheet>

为什么你想忽略它,而不是使用它?名称空间不可靠吗?很好,我想我不需要忽略它。我只需要将元素与名称空间匹配,我不确定如何匹配,因为它没有像我在其他示例中看到的那样被赋予别名。也许您应该将
exclude result prefixes=“class”
添加到样式表元素中。建议不错。我将添加它作为可能的改进(取决于OP的意图)。