简体   繁体   English

创建 xmlns:xsi 命名空间和属性

[英]Create xmlns:xsi namespace and attribute

I want to create the following element:我想创建以下元素:

<exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd">

If I use something like this:如果我使用这样的东西:

<xsl:element name="excercises">
<xsl:attribute name="xmlns:xsi" namespace="http://www.w3.org/2001/XMLSchema-instance"/>

Then it creates soemthing like this:然后它会创建这样的东西:

<excercises xp_0:xsi="" xmlns:xp_0="http://www.w3.org/2001/XMLSchema-instance">

Which doesn't look like what I want...这看起来不像我想要的......

Try the following instead:请尝试以下操作:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="xml">
        <xsl:element name="exercises">
            <xsl:attribute name="xsi:noNamespaceSchemaLocation">mySchema.xsd</xsl:attribute>
            some value
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

The key concern is to declare the xsi namespace in the declaration.关键问题是在声明中声明 xsi 命名空间。

I've just made up the template match on just to test.我刚刚制作了模板匹配只是为了测试。

Here is how this can be done :这是如何做到的

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 exclude-result-prefixes="xsi">
    <xsl:output omit-xml-declaration="yes"/>
    <!--                                   -->
    <xsl:template match="/">
      <exercises  xsi:noNamespaceSchemaLocation="mySchema.xsd"/>
    </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any source XML document (not used), the wanted result is produced:当此转换应用于任何源 XML 文档(未使用)时,会产生所需的结果:

<exercises xsi:noNamespaceSchemaLocation="mySchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

It is not necessary to use <xsl:attribute> in your case , however if necessary, it can be used without any problem:在您的情况下没有必要使用<xsl:attribute> ,但是如果有必要,它可以毫无问题地使用:

    <xsl:attribute name="xsi:noNamespaceSchemaLocation">
      <xsl:value-of select="'mySchema.xsd'"/>
    </xsl:attribute>

Do note that it is a good practice to simply define the necessary namespaces at the <xsl:stylesheet> element so that they can readily be (re)used everywhere they are needed.请注意,在<xsl:stylesheet>元素中简单地定义必要的名称空间是一种很好的做法,这样它们就可以在需要的地方随时(重新)使用。 THis is especially useful, if a given namespace will be needed on more than one generated element or attribute.如果在多个生成的元素或属性上需要给定的命名空间,这将特别有用。

In this case it is also good to specify all such prefixes in the value of the exclude-result-prefixes attribute so that the namespaces will not be automatically propagated on all literal result elements.在这种情况下,最好在exclude-result-prefixes属性的值中指定所有此类前缀,这样命名空间就不会在所有文字结果元素上自动传播。

You could simply use:-你可以简单地使用:-

<exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd">

Directly in your XSL, that would work, you only really need xsl:element if can't hard code the tag name.直接在您的 XSL 中,这会起作用,如果不能对标签名称进行硬编码,您只需要 xsl:element。 Similarly with attributes you can add them directly unless you need to make conditional.与属性类似,您可以直接添加它们,除非您需要设置条件。

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

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