我有这个输入 XML,我需要对其应用 XSL 并将其转换为另一个更高版本的 XML。让我们说V3。所以输入 XML 的版本是 V1。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:NS1="http://www.test1/Error/v1"
xmlns:NS2="http://www.test1/Error/schema/SCRIPT"
xmlns:tns="http://www.test1/webservice/Service/v1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<tns:Sample>
<NS2:Message release="006" version="010">
<NS2:Header>
<NS2:TestMessage>1</NS2:TestMessage>
</NS2:Header>
<NS2:Body>
<NS2:SampleTx>
<NS2:Element1>
<NS2:IdName>
<NS2:ID>
<NS2:IDValue>114</NS2:IDValue>
</NS2:ID>
</NS2:IdName>
</NS2:Element1>
</NS2:SampleTx>
</NS2:Body>
</NS2:Message>
</tns:Sample>
</soapenv:Body>
</soapenv:Envelope>
我申请的XSL是
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:djh="http://www.test1/webservice/Service/v1"
xmlns:NS1="http://www.test1/Error/v1"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output indent="yes"/>
<xsl:template match="@*|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="djh:*">
<xsl:element name="{name()}" namespace="http://www.test1/webservice/Service/v3">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我得到的输出是
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<tns:sendCancelRx xmlns:tns="http://www.test1/webservice/Service/v3">
<NS2:Message release="006" version="010"
xmlns:NS2="http://www.ncpdp.org/schema/SCRIPT">
<NS2:Header>
<NS2:TestMessage>1</NS2:TestMessage>
</NS2:Header>
<NS2:Body>
<NS2:SampleTx>
<NS2:Element1>
<NS2:IdName>
<NS2:ID>
<NS2:IDValue>114</NS2:IDValue>
</NS2:ID>
</NS2:IdName>
</NS2:Element1>
</NS2:SampleTx>
</NS2:Body>
</NS2:Message>
</tns:sendCancelRx>
</soapenv:Body>
</soapenv:Envelope>
它从 xmlns:NS1="http://www.test1/Error/v1"xmlns:NS2="http://www.test1/Error/schema/SCRIPT"xmlns 中剥离所有命名空间声明:tns="http://www.test1/webservice/Service/v1"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3. org/2001/XMLSchema 实例”
我想要的输出是
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:NS1="http://www.test1/Error/v3"
xmlns:NS2="http://www.test1/Error/schema/SCRIPT"
xmlns:tns="http://www.test1/webservice/Service/v3"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<tns:Test>
<NS2:Message release="006" version="010">
<NS2:Header>
<NS2:TestMessage>1</NS2:TestMessage>
</NS2:Header>
<NS2:Body>
<NS2:SampleTx>
<NS2:Element1>
<NS2:IdName>
<NS2:ID>
<NS2:IDValue>114</NS2:IDValue>
</NS2:ID>
</NS2:IdName>
</NS2:Element1>
</NS2:SampleTx>
</NS2:Body>
</NS2:Message>
</tns:Test>
</soapenv:Body>
</soapenv:Envelope>
如果有人能让我知道我的 XSL 中缺少什么,我将不胜感激。
最佳答案
以下 XSLT 保留所有命名空间声明(已使用和未使用)并将 tns namespace-uri 更改为 http://www.test1/webservice/Service/v3:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:djh="http://www.test1/webservice/Service/v1"
xmlns:tns="http://www.test1/webservice/Service/v3"
xmlns:NS1="http://www.test1/Error/v1"
xmlns:NS2="http://www.test1/Error/schema/SCRIPT"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output indent="yes"/>
<xsl:template match="@*|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="djh:*">
<!--reconstruct an element using the new v3 namespace,
but use the same namespace-prefix "tns"-->
<xsl:element name="tns:{local-name()}"
namespace="http://www.test1/webservice/Service/v3">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<!--copy all of the namespace declarations,
except for "tns" - which is v1 in the source XML -->
<xsl:copy-of select=
"namespace::*
[not(name()='tns')]"/>
<!--copy the namespace declaration for "tns" from the XSLT,
which is v3, and add it to the element-->
<xsl:copy-of select=
"document('')/*/namespace::*[name()='tns']"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
它使用 Saxon 9.x 生成以下输出(不保留 Xalan 未使用的 namespace ,但这显然是由于 XALANJ-1959 造成的):
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:NS1="http://www.test1/Error/v1"
xmlns:NS2="http://www.test1/Error/schema/SCRIPT"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tns="http://www.test1/webservice/Service/v3">
<soapenv:Body>
<tns:Sample>
<NS2:Message release="006" version="010">
<NS2:Header>
<NS2:TestMessage>1</NS2:TestMessage>
</NS2:Header>
<NS2:Body>
<NS2:SampleTx>
<NS2:Element1>
<NS2:IdName>
<NS2:ID>
<NS2:IDValue>114</NS2:IDValue>
</NS2:ID>
</NS2:IdName>
</NS2:Element1>
</NS2:SampleTx>
</NS2:Body>
</NS2:Message>
</tns:Sample>
</soapenv:Body>
</soapenv:Envelope>
关于xml - XSLT 转换从 soap :env 中剥离所有命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118699/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c