【问题标题】:add prefix namespace only in xsd:schema with xslt仅在带有 xslt 的 xsd:schema 中添加前缀命名空间
【发布时间】:2018-07-06 15:11:39
【问题描述】:

我正在尝试将以下前缀xmlns:nr0="http://NamespaceTest.com/balisesXrm" 添加到我的xsd:schema,而不更改我的XSD 文档。 我试过这个:

<xsl:template match="xsd:schema">
 <xsl:element name="nr0:{local-name()}" namespace="http://NamespaceTest.com/balisesXrm">
   <xsl:copy-of select="namespace::*"/>
   <xsl:apply-templates select="node() | @*"/>
 </xsl:element>    
</xsl:template>

但这会产生两个问题: 1 - 我的架构因名称更改为无效:&lt;nr0:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SCCOAMCD="urn:SCCOA-schemaInfo" xmlns="urn:SBEGestionZonesAeriennesSYSCA-schema"&gt;

2 - 我在 XML 模式中创建的所有元素都已被删除。

我怎样才能保留我的元素并将前缀添加到我的根?

【问题讨论】:

    标签: xml xslt xsd xsd-validation


    【解决方案1】:

    对于您的第一个问题,您的代码当前正在创建一个元素,而您确实想要创建一个命名空间声明。

    您可以做的,就是创建一个带有所需命名空间声明的新 xsd:schema 元素,并复制所有现有的。

    <xsl:template match="xsd:schema">
      <xsd:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm">
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates select="@*|node()"/>
      </xsd:schema>   
    </xsl:template>
    

    或者,如果您可以使用 XSLT 2.0,您可以使用 xsl:namespace 并执行此操作...

    <xsl:template match="xsd:schema">
      <xsl:copy>
        <xsl:namespace name="nr0" select="'http://NamespaceTest.com/balisesXrm'" />
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>   
    </xsl:template>
    

    xsl:copy 在这种情况下复制现有的命名空间)

    对于您的第二个问题,您需要将标识模板添加到您的样式表中(如果您还没有)

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

    试试这个 XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        version="2.0">
    
        <xsl:output method="xml" indent="yes" />
    
        <xsl:template match="@*|node()">
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:template>
    
        <xsl:template match="xsd:schema">
          <xsd:schema xmlns:nr0="http://NamespaceTest.com/balisesXrm">
            <xsl:copy-of select="namespace::*"/>
            <xsl:apply-templates select="@*|node()"/>
          </xsd:schema>   
        </xsl:template>
    
        <!-- Alternative code for XSLT 2.0 -->
        <xsl:template match="xsd:schema">
          <xsl:copy>
            <xsl:namespace name="nr0" select="'http://NamespaceTest.com/balisesXrm'" />
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>   
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      相关资源
      最近更新 更多