简体   繁体   English

xsd的NameSpace前缀:任何元素,并使用XSLT添加名称空间前缀

[英]NameSpace prefix for xsd:any elements and adding namespace prefix with XSLT

I am having trouble in working with the name space. 我在使用名称空间时遇到麻烦。

I get the input as below. 我得到如下输入。

  <?xml version="1.0" encoding="UTF-8"?>

      <Org xmlns="http://mysample.org" >
        <Dept>
            <Person>
                <FirstName>Sample </FirstName>
                <LastName> Sampel L </LastName>
                <Address>
                    <Street>Sample Street</Street>
                    <House>45 Block C </House>
                    <State>Kentucky</State>
                    <AddExtension>
                        <ns3:LandMark xmlns:ns3="http://mysample.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                            <ns3:POI tag="temp">Sample POI </ns3:POI>
                        </ns3:LandMark>                         
                    </AddExtension>
            </Person>
        </Dept>
      </Org>

I need to add the namespace prefix to all the elements in this XML. 我需要将名称空间前缀添加到此XML中的所有元素。

I tried the below XSL: 我尝试了以下XSL:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:ns3="http://mysample.org">

    <xsl:output omit-xml-declaration="no" indent="yes" />
    <xsl:strip-space elements="*" />

    <!-- <xsl:template match="*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> 
        </xsl:copy> </xsl:template> -->

    <xsl:template match="*"> 
        <xsl:element name="ns3:{name()}" namespace="http://mysample.org">
            <xsl:copy-of select="@*"></xsl:copy-of>
            <xsl:copy-of select="namespace::*" />
            <xsl:apply-templates select="node()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

But it is giving problem because of the AddExtension data in the input XML " 但是由于输入XML中的AddExtension数据“

Note: the Data inside the "AddExtension" is coming based on the xsd:any tag as per the scema. 注意:“ AddExtension”中的数据是根据scema基于xsd:any标签来的。 So it will be different data for different input XMLs. 因此,对于不同的输入XML,它将是不同的数据。

How can I overcome this? 我该如何克服?

Plese help. 请帮助。

Try this template instead: 尝试使用以下模板:

<xsl:template match="*"> 
    <xsl:element name="ns3:{local-name()}" namespace="{namespace-uri()}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()" />
    </xsl:element>
</xsl:template>

Your current attempt is writing out every element in the document in the http://mysample.org namespace, with an explicit (I mean non-empty) namespace prefix. 您当前的尝试是写出http://mysample.org命名空间中文档中的每个元素,并带有一个显式的(我的意思是非空的)命名空间前缀。 I suspect you really want this to happen only for elements which are already in that namespace. 我怀疑您真的希望仅对于该名称空间中已经存在的元素才发生这种情况。 So 所以

  1. Change your existing template from match="*" to match="ns3:*" . 将现有模板从match="*"更改为match="ns3:*"
  2. Add a template to handle elements in other namespaces (use match="*" for that one) that just copies them through unchanged. 添加一个模板来处理其他命名空间中的元素(对于该命名空间,请使用match="*" ),该模板只需将它们复制不变即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM