【问题标题】:Convert a XML to another XML将一个 XML 转换为另一个 XML
【发布时间】:2017-05-18 17:53:27
【问题描述】:

我是 XSLT 的新手

我有一个输入 xml

<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<urn:LookupRecords xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:urn="urn:RedIron.RetailRepository.Services.SearchService">
<urn:query>
<a:Headers>
<a:SearchHeader>
<a:SearchHeader>
<a:SearchHeader>
</a:Headers>
<a:Params>
<arr:KeyValueOfguidArrayOfQueryParametertmL6yAXy>
</a:Params>
</urn:query>
</urn:LookupRecords>
</soapenv:Body>
</soapenv:Envelope>

输出的 XML 是

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService">
<GetRecordResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="urn:RedIron.RetailRepository.Core">
<a:Exception i:nil="true"/>
<a:ResponseHeaders>
<a:SearchResults>
<a:StatusCode>Success</a:StatusCode>
<a:StatusCodeReason i:nil="true"/>
</GetRecordResult>
</GetRecordResponse>
</s:Body>
</s:Envelope>

现在我正在尝试使用 XSLT 更改转换,例如

<s:Envelope>
<xsl:attribute name="xmlns:s"><xsl:value-of select="xmlns:soapenv"/></xsl:attribute>
<s:Body>
</s:Body>
</s:Envelope> 

但收到错误“格式不正确:元素“s:Envelope”的前缀“s”未绑定。”

你能帮忙吗

【问题讨论】:

  • xmlns 不是属性。您不能使用 &lt;xsl:attribute&gt; 创建它们。您错过了有关 XML 名称空间的基本知识,应该花时间阅读它们。另外,请展示更多您当前的 XSLT。
  • 这里有一些学习命名空间的资源:@​​987654321@ 和 jclark.com/xml/xmlns.htm
  • 发布整个 XSLT 会很有用,因为您似乎缺乏有关它的基本知识。
  • 用整个 XSLT 编辑。请告诉我是否有其他方法可以转换它。
  • 这几乎不是“整个 XSLT”。请查看:minimal reproducible example.

标签: .net xml xslt


【解决方案1】:

如前所述,命名空间声明不是属性,不能使用xsl:attribute 指令创建。为了编写 XSLT sn-p 中显示的文字结果元素,您需要将代码更改为:

<s:Envelope>
<xsl:attribute name="xmlns:s"><xsl:value-of select="xmlns:soapenv"/></xsl:attribute>
<s:Body>
</s:Body>
</s:Envelope> 

到:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
</s:Body>
</s:Envelope> 

请注意,该声明在包含它的节点的所有后代的范围内(因此无需为 s:Body 元素重复它)。大多数情况下,您会将所有命名空间声明包含在顶级 xsl:stylesheet 标记中。

【讨论】:

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