选择元素时忽略 XML 命名空间前缀 - XSLT [英] Ignoring XML namespace prefix while selecting elements - XSLT

查看:38
本文介绍了选择元素时忽略 XML 命名空间前缀 - XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML 版本 1

<inboundData xmlns="urn:college:names:ws:docexchange">
<Root>
        <College Version="5.0" xmlns:cidx="urn:abc:names:specification:col:schema:all:5:0" xmlns="urn:abc:names:specification:col:schema:all:5:0">
           <Header>
              <Address>
                 <AddressLine1>4600 Big Tree Way</AddressLine1>
              </Address>
           </Header>
    </College>
</Root>
</inboundData>

XML 版本 2

<inboundData xmlns="urn:college:names:ws:docexchange">
<Root>
        <ns1:College Version="5.0" xmlns:ns1="urn:abc:names:specification:col:schema:all:5:0">
           <ns1:Header>
              <ns1:Address>
                 <ns1:AddressLine1>4600 Big Tree Way</ns1:AddressLine1>
              </ns1:Address>
           </ns1:Header>
    </ns1:College>
</Root>
</inboundData>

XSL 代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="urn:abc:names:specification:col:schema:all:5:0" xmlns:b="urn:college:names:ws:docexchange">
<xsl:template match="/">
    <xsl:copy-of select="b:inboundData/b:College/*"/>
</xsl:template>
</xsl:stylesheet>

XSL 代码中的更正.(看了汉森的回复才意识到错误)

Correction in the XSL code. (Realized the error after reading Hansen's response)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="urn:abc:names:specification:col:schema:all:5:0" xmlns:b="urn:college:names:ws:docexchange">
<xsl:template match="/">
    <xsl:copy-of select="b:inboundData/b:Root/*"/>
</xsl:template>
</xsl:stylesheet>

XSL 代码适用于 XML 版本 1.由于额外的命名空间ns1",它不适用于类型 2.如何使 xsl 代码适用于这两个版本?请赐教!

The XSL code works well for XML version 1. Due to the extra namespace "ns1", it does not work for type 2. How can I make the xsl code work for both these versions? Kindly enlighten me!

推荐答案

您可以通过元素的本地名称来匹配元素,例如

You can match elements by their local name, like

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:a="urn:abc:names:specification:col:schema:all:5:0" 
    xmlns:b="urn:college:names:ws:docexchange">
  <xsl:template match="/">
    <xsl:copy-of select="b:inboundData/*[local-name()='College']/*"/>
  </xsl:template>
</xsl:stylesheet>

这篇关于选择元素时忽略 XML 命名空间前缀 - XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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