xml 创建 xmlns:xsi 命名空间和属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/424148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create xmlns:xsi namespace and attribute
提问by eddy147
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 doesnt look similar to what i want...
这看起来不像我想要的......
采纳答案by Kev
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.
我刚刚制作了模板匹配只是为了测试。
回答by Dimitre Novatchev
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 appliedon 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>elementso that they can readily be (re)used everywhere they are needed. THis is especially useful, if a given namespace will be needed on more than one generated element or attribute.
请注意,在<xsl:stylesheet>元素中简单地定义必要的命名空间是一个很好的做法,这样它们就可以在需要的地方随时(重新)使用。如果多个生成的元素或属性需要给定的命名空间,这将特别有用。
In this case it is also good to specify all such prefixes in the value of the exclude-result-prefixesattributeso that the namespaces will not be automatically propagated on all literal result elements.
在这种情况下,最好在exclude-result-prefixes属性值中指定所有此类前缀,这样命名空间就不会在所有文字结果元素上自动传播。
回答by AnthonyWJones
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. Similarly with attributes you can add them directly unless you need to make conditional.
直接在您的 XSL 中,这会起作用,如果不能硬编码标签名称,您只需要 xsl:element 。与属性类似,您可以直接添加它们,除非您需要设置条件。

