Choosing between different namespaces in XSLT

放肆的年华 提交于 2019-12-02 07:00:38

问题


The XML I am expecting is supposed to be only one url/urn (xmlns:urn="urn:123:456") like below:

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

When used with the below namespace it's OK:

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

But recently I am receiving a different document with the same structure as before, only difference is the namespace like below:

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

My question is, is there any way that I can support both in the same XSLT file

Below is a sample of my XSLT file:

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


You could declare both namespaces, e.g.

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

Then use a union expression for your matches and selections, for example:

<xsl:template match="/ns1:Document | /ns2:Document">


来源:https://stackoverflow.com/questions/35618120/choosing-between-different-namespaces-in-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!