我正在尝试执行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>
预先感谢您的帮助。
注意:这样的更改纯粹是表面上的-与XML文档的语义无关。
由于您使用的是XSLT 2.0,因此请使用xsl:namespace
XML文档最外层元素上的指令,并在其中声明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:
,这同样是一种装饰性的措施。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句