Xml 复制元素的所有属性[不仅是属性值,而且是整个属性列表]
我有一个场景,我想从输入XML中的XML标记复制所有属性,包括名称空间[这里我指的是整个属性列表和名称空间列表,而不仅仅是属性的值] 对于Ex:Xml 复制元素的所有属性[不仅是属性值,而且是整个属性列表],xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有一个场景,我想从输入XML中的XML标记复制所有属性,包括名称空间[这里我指的是整个属性列表和名称空间列表,而不仅仅是属性的值] 对于Ex: <?xml version="1.0" encoding="UTF-8"?> <enricher> <result> <xbrl xmlns="http://www.xbrl.org/2003/instance"
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
正文
2000-07-14
输出XML:
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
正文
2000-07-14
输出应该有
标记,带有
编辑的所有名称空间和属性:在您发现实际问题后,我还更改了样式表。同样,您的输入不是格式良好的XML,因为未定义命名空间:
`xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
XML输入
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context xmlns="http://www.xbrl.org/2003/instance" xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1" xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1" id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
XML输出
<enricher>
<result>
<xbrl xmlns="http://www.xbrl.org/2003/instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1"
xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1"
xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd
http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrl>
</result>
</enricher>
<?xml version="1.0" encoding="UTF-8"?>
<enricher>
<result>
<xbrlresp xmlns="http://www.xbrl.org/2003/instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dnb.com/IDP/Product/Common/Vers1 ../Common/ProductCommonTaxonomy.xsd http://www.dnb.com/IDP/Common/Vers1 ../../Data/Common/CommonTaxonomy.xsd">
<context xmlns="http://www.xbrl.org/2003/instance" xmlns:idp-com="http://www.dnb.com/IDP/Common/Vers1" xmlns:idp-enumcom="http://www.dnb.com/IDP/Common/Enumeration/Common/Vers1" id="defaultI">
<entity>
<identifier scheme="http://www.dnb.com">Text</identifier>
</entity>
<period>
<instant>2000-07-14</instant>
</period>
</context>
</xbrlresp>
</result>
</enricher>
正文
2000-07-14
扩展,如果出于美观原因,您希望将xbrl
元素的命名空间声明复制到您正在创建的新元素上,则可以使用
<xsl:template match="xb:xbrl">
<xsl:element name="xbrlresp" namespace="http://www.xbrl.org/2003/instance">
<xsl:copy-of select="namespace::*" />
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
的
副本将命名空间节点从输入xbrl
元素复制到生成的xbrresp
,这将导致序列化程序添加命名空间声明。@AkhilJacobJohn Hi,也经过编辑-这是实际的答案。使用“我的代码”时会复制所有名称空间。顺便说一句,名称空间不是一个属性。@AkhilJacobJohn P.S.关于Stackoverflow,接受对您有帮助的答案被认为是一种很好的方式。所以,请接受一个答案——如果它解决了你的问题,也请接受这个答案。@AkhilJacobJohn请参阅Ian关于如何将所有名称空间保持在一起的答案。我不认为我需要编辑我的文章并在那里重复。@AkhilJacobJohn请避免添加更多的评论来提出新的问题。目前我们已经提供了充分的建议。我建议您自己花几分钟时间考虑一下您的问题,如果您还有其他问题,请提出一个干净的新问题。@AkhilJacobJohn您是否按照Matthias的建议更正了输入XML以声明xsi
命名空间前缀?我怀疑如果您的输入名称空间格式不正确,事情会变得混乱。@AkhilJacobJohn然后apply templates
中的@*
应该捕获该属性,而match=“@*|node()”
标识模板将复制该属性。您是否有任何其他模板可能与xsi:schemaLocation
属性节点相匹配并妨碍了您的工作?@AkhilJacobJohnxsi:schemaLocation
肯定是。既然我一直在写,让我提醒你,你还没有接受你最后一个问题的答案!嗨,为什么要更改这个7年问题的名称空间URI?我回滚了您的编辑(并拒绝了对我答案的更改),因为您没有给出任何解释。