我正在尝试进行xslt转换但是我没有得到我想要的结果。我需要将几个带有前缀的命名空间插入到xml消息中。
这是我得到的消息:
<Operation>
<SessionId>?</SessionId>
<appUser>
<number1>?</number1>
<field2>?</field2>
<Properties>
<!--Zero or more repetitions:-->
<KeyValue>
<Key>?</Key>
<Value>?</Value>
</KeyValue>
</Properties>
<value3>?</value3>
</appUser>
</Operation>
这是我的转变:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="ser:{name()}" namespace="http://www.url.com/Services">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[ancestor::appUser]">
<xsl:element name="mp:{name()}" namespace="http://schemas.data.org/myPath">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[ancestor-or-self::KeyValue ]">
<xsl:element name="arr:{name()}" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<xsl:copy-of select="namespace::*" />
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我得到以下内容:
<ser:Operation xmlns:ser="http://www.url.com/Services">
<ser:SessionId>?</ser:SessionId>
<ser:appUser>
<mp:number1 xmlns:mp="http://schemas.data.org/myPath">?</mp:number1>
<mp:field2 xmlns:mp="http://schemas.data.org/myPath">?</mp:field2>
<mp:Properties xmlns:mp="http://schemas.data.org/myPath">
<!--Zero or more repetitions:-->
<arr:KeyValue xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<arr:Key>?</arr:Key>
<arr:Value>?</arr:Value>
</arr:KeyValue>
</mp:Properties>
<mp:value3 xmlns:mp="http://schemas.data.org/myPath">?</mp:value3>
</ser:appUser>
</ser:Operation>
但我不喜欢在每个元素中看到命名空间声明xmlns:mp =“http://schemas.data.org/myPath”。
那么,我可以修改我的xslt以获得类似下面的内容吗?
<ser:Operation xmlns:ser="http://www.url.com/Services" xmlns:mp="http://schemas.data.org/myPath">
<ser:SessionId>?</ser:SessionId>
<ser:appUser>
<mp:number1 >?</mp:number1>
<mp:field2>?</mp:field2>
<mp:Properties>
<!--Zero or more repetitions:-->
<arr:KeyValue xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<arr:Key>?</arr:Key>
<arr:Value>?</arr:Value>
</arr:KeyValue>
</mp:Properties>
<mp:value3>?</mp:value3>
</ser:appUser>
</ser:Operation>
提前感谢您的帮助。
答案 0 :(得分:1)
注意:这样的更改纯粹是装饰性的 - 它与XML文档的语义无关。
由于您使用的是XSLT 2.0,因此请在XML文档的最外层元素上使用xsl:namespace
指令,并在那里声明mp:
命名空间。这样做的方法是为Operation
编写单独的模板:
<xsl:template match="/*">
构造一个以ser:
<xsl:element name="ser:{name()}">
并向此元素添加另一个命名空间节点:
<xsl:namespace name="mp" select="'http://schemas.data.org/myPath'"/>
然后,XSLT处理器将避免在输出中序列化冗余命名空间声明。
此外,如果在xsl:stylesheet
元素中声明所有名称空间,而不是代码中的其他位置,则样式表更具可读性。
<强>样式表强>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ser="http://www.url.com/Services"
xmlns:mp="http://schemas.data.org/myPath"
xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:element name="ser:{name()}">
<xsl:namespace name="mp" select="'http://schemas.data.org/myPath'"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="ser:{name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="appUser/*">
<xsl:element name="mp:{name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[ancestor-or-self::KeyValue ]">
<xsl:element name="arr:{name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
XML输出
<ser:Operation xmlns:mp="http://schemas.data.org/myPath"
xmlns:ser="http://www.url.com/Services">
<ser:SessionId>?</ser:SessionId>
<ser:appUser>
<mp:number1>?</mp:number1>
<mp:field2>?</mp:field2>
<mp:Properties><!--Zero or more repetitions:-->
<arr:KeyValue xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<arr:Key>?</arr:Key>
<arr:Value>?</arr:Value>
</arr:KeyValue>
</mp:Properties>
<mp:value3>?</mp:value3>
</ser:appUser>
</ser:Operation>
当然,你可以在最外面的元素(arr:
)上声明所有名称空间,这也是一个美化措施。