XSLT to copy element without namespace

☆樱花仙子☆ 提交于 2019-12-24 13:25:45

问题


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.

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

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

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>

This is the 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>

回答1:


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.

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.




回答2:


You can use:

<xsl:copy-of select="bla" copy-namespaces="no"></xsl:copy-of>

sorry...just noticed you are using XSLT 1, so copy-namespaces is not supported...my bad



来源:https://stackoverflow.com/questions/21397126/xslt-to-copy-element-without-namespace

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