在XSLT中选择不同的命名空间

时间:2016-02-25 04:07:22

标签: xslt-1.0

我期待的XML应该只有一个url / urn(xmlns:urn =" urn:123:456"),如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
            xmlns:urn="urn:123:456"
            xmlns:exsl="http://exslt.org/common"
            extension-element-prefixes="exsl" exclude-result-prefixes="urn">

当与下面的命名空间一起使用时,它可以:

<Document xmlns="123:456" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

但是最近我收到了一个与之前结构相同的不同文档,唯一不同的是下面的名称空间:

<Document xmlns="789:123" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

我的问题是,有什么方法可以在同一个XSLT文件中同时支持

以下是我的XSLT文件示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:urn="urn:123:456"
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl" exclude-result-prefixes="urn">

  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/urn:Document">
    <Profiles>
      <xsl:apply-templates select="*"/>
    </Profiles>
  </xsl:template>

  <xsl:template match="urn:File">
    <File>
      <FileId>
        <xsl:value-of select="urn:Id"/>
      </FileId>
      <FileDate>
        <xsl:value-of select="urn:Created"/>
      </FileDate>
    </File>
  </xsl:template>

  <xsl:template match="urn:Employee">
    <Information>
      <EmpName>
        <xsl:value-of select="urn:Personal/Name"/>
      </EmpName>
      <Age>
        <xsl:value-of select="urn:Personal/Age"/>
      </Age>
      .
      .
      .
    </Information>
  </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

您可以声明两个名称空间,例如

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns1="urn:123:456"
xmlns:ns2="urn:789:123"
exclude-result-prefixes="ns1 ns2">

然后为您的匹配和选择使用联合表达式,例如:

<xsl:template match="/ns1:Document | /ns2:Document">
相关问题