在 XSLT 中的不同名称空间之间进行选择 [英] Choosing between different namespaces in XSLT
本文介绍了在 XSLT 中的不同名称空间之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我期望的 XML 应该只有一个 url/urn (xmlns:urn="urn:123:456"),如下所示:
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">
我的问题是,有没有什么办法可以在同一个 XSLT 文件中同时支持两者
My question is, is there any way that I can support both in the same XSLT file
以下是我的 XSLT 文件示例:
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>
推荐答案
您可以声明两个命名空间,例如
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">
这篇关于在 XSLT 中的不同名称空间之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文