XSLT 复制没有命名空间的元素 [英] XSLT to copy element without namespace

查看:39
本文介绍了XSLT 复制没有命名空间的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先证明我不是特别擅长 XSLT.但我要做的是将 3 个 XML 文档复制到一个父文档中.

I'll firstly qualify that I'm not particularly good at XSLT. But what I'm trying to do is to copy 3 XML documents into one parent document.

XSLT 工作正常,除了父节点使用 xmlns="" 属性打印,这导致我的验证失败.

The XSLT works fine, except that the parent nodes are being printed with xmlns="" attribute which is causing my validations to fail.

所以我的问题是如何从单独的文档中复制这些元素而不添加 xmlns 属性?

So my question is how do I copy these elements out from the separate documents without it adding that xmlns attribute?

这是到目的地的输出示例 - 请注意,我希望它不打印 xmlns="".

This is sample of the output to the destination - note I want it to not print the xmlns="".

<Feed xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6"
name="Officeworks" incremental="false"
extractDate="2014-01-28T14:42:12+11:00">
  <Brands xmlns="">
    <Brand>
      <Name>Panasonic</Name>
      <ExternalId>12345</ExternalId>
    </Brand>

这是 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex"> 

<xsl:template match="/">
  <xsl:variable name="dateNow" select="ex:date-time()"/>
  <Feed xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6" name='Officeworks' incremental='false'>
  <xsl:attribute name="extractDate"><xsl:value-of select="$dateNow" /></xsl:attribute>
  <xsl:copy-of select="document('@dataload.bv.xml.out.tmp@/bv_brands_xml.001.xml')/Brands"/>
  <xsl:copy-of select="document('@dataload.bv.xml.out.tmp@/bv_categories_xml.001.xml')/Categories"/>
  <xsl:copy-of select="document('@dataload.bv.xml.out.tmp@/bv_products_xml.001.xml')/Products"/>
  </Feed>
</xsl:template>

</xsl:stylesheet>

推荐答案

xmlns="" 是必需的,因为您要断言默认命名空间(使用 xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6") 然后输出不在该命名空间中的节点.为了让 XML 正确地表示它,它必须取消默认命名空间.

The xmlns="" is required because you're asserting a default namespace (with xmlns="http://www.bazaarvoice.com/xs/PRR/ProductFeed/5.6") and then outputting nodes which are not in that namespace. For XML to represent that properly, it has to cancel the default namespace.

您还没有告诉我们验证是如何失败的.如果由于这些节点应该在指定的命名空间中而失败,则它们需要在复制之前位于该命名空间中,或者需要将 xsl:copy-of 操作替换为显式重构的操作通过提取它们的本地名称并使用该名称和所需的名称空间作为 xsl:element 操作的参数来实现它们.相关下方列出的项目会告诉您更多相关信息.

You haven't told us how validation is failing. If it's failing because those nodes should be in the specified namespace, they need to either be in that namespace before you copy them, or you need to replace the xsl:copy-of operation with one that explicitly reconstructs them by extracting their localname and using that and the desired namespace as parameters of an xsl:element operation. The items listed at right under Related will tell you more about this.

这篇关于XSLT 复制没有命名空间的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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