我试图定义一个可以在后续元素定义中引用的属性。
<xs:attribute name="ridref" type="xs:string"/>
后来我是这样用的:
<xs:element name="coordRegRef">
<xs:complexType>
<!--xs:attribute name="ridref" type="xs:string"/ this works but I want to use ref -->
<xs:attribute ref="ridref" use="required"/>
</xs:complexType>
</xs:element>
XSD 使用 xmllint 编译良好
xmllint --schema pc-ar.xsd pc-ar.xml
但是 xmllint 说
pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef', attribute 'ridref': The attribute 'ridref' is not allowed.
pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef': The attribute '{http://pc-ar.xsd}ridref' is required but missing.
这意味着我的 XML 文件必须为 ridref 使用命名空间
<coordRegRef fix:ridref="111"/>
(确实有效,但不受欢迎)而不是
<coordRegRef ridref="111"/>
为什么?
我的 XSD
<?xml version="1.0"?>
<!-- validate: xmllint - -schema pc-ar.xsd pc-ar.xml -->
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://pc-ar.xsd"
elementFormDefault="qualified"
xmlns="http://pc-ar.xsd"
xmlns:fix="http://pc-ar.xsd">
<!--xs:attribute name="rid" type="xs:integer"/-->
<!--xs:attributeGroup name="ridGroup">
<xs:attribute name="rid" type="xs:integer">
<xs:annotation>
<xs:documentation>entry id</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup-->
<xs:element name="name" type="xs:string"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="idref" type="xs:IDREF"/>
<xs:attribute name="ridref" type="xs:string"/>
<xs:element name="coordRegRef">
<xs:complexType>
<!--xs:attribute name="ridref" type="xs:string"/works-->
<xs:attribute ref="ridref" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="assetRegRef">
<xs:complexType>
<xs:sequence>
<xs:element name="ridref2" type="xs:string"/>
</xs:sequence>
<!--xs:attribute name="ridref" type="xs:string"/works-->
<xs:attribute name="ridref" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="lat">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:minInclusive value="-40"/>
<xs:maxInclusive value="-30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="lon" type="xs:decimal"/>
<xs:complexType name="coordRegTableType">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="lat"/>
<xs:element ref="lon"/>
</xs:sequence>
<xs:attribute name="rid" type="xs:string"/>
<!--xs:attribute ref="fix:id" use="required"/-->
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="assetRegTableType">
<xs:sequence>
<xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="coordRegRef"/>
<xs:element ref="assetRegRef"/>
</xs:sequence>
<xs:attribute name="rid" type="xs:string"/>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="coordReg" type="fix:coordRegTableType">
<xs:unique name="coordRegNameUniq">
<xs:selector xpath="*"/>
<xs:field xpath="fix:name"/>
</xs:unique>
</xs:element>
<xs:element name="assetReg" type="assetRegTableType">
<xs:unique name="assetRegNameUniq">
<xs:selector xpath="fix:entry"/>
<xs:field xpath="fix:name"/>
</xs:unique>
</xs:element>
<xs:element name="assetExchange">
<xs:complexType>
<xs:sequence>
<xs:element ref="coordReg"/>
<xs:element ref="assetReg"/>
</xs:sequence>
<xs:attribute name="rid" type="xs:integer"/>
<!--xs:attribute ref="id" use="required"/-->
</xs:complexType>
<xs:key name="coordRegKey">
<xs:selector xpath="fix:coordReg/fix:entry"/>
<xs:field xpath="@rid"/>
</xs:key>
<xs:key name="assetRegKey">
<xs:selector xpath="fix:assetReg/fix:entry"/>
<xs:field xpath="@rid"/>
</xs:key>
<xs:keyref name="assetRegKeyRef" refer="assetRegKey">
<xs:selector xpath=".//fix:assetRegRef"/>
<xs:field xpath="@ridref"/>
</xs:keyref>
<xs:keyref name="assetRegKeyRef2" refer="assetRegKey">
<!--xs:selector xpath=".//fix:assetRegRef"/works-->
<!--xs:selector xpath="./*/*/fix:assetRegRef"/works-->
<!--xs:selector xpath="./*/fix:entry/fix:assetRegRef"/works-->
<!--xs:selector xpath="./*/*/fix:assetRegRef"/works-->
<xs:selector xpath=".//fix:assetRegRef"/> <!-- any assetRefRef -->
<xs:field xpath="fix:ridref2"/>
</xs:keyref>
<xs:keyref name="coordRegKeyRef" refer="coordRegKey">
<xs:selector xpath=".//fix:coordRegRef"/>
<xs:field xpath="@ridref"/>
</xs:keyref>
</xs:element>
</xs:schema>
我的 XML 测试文件
<?xml version="1.0"?>
<assetExchange
xmlns="http://pc-ar.xsd"
xmlns:fix="http://pc-ar.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pc-ar.xsd pc-ar.xsd"
rid="1">
<coordReg>
<entry id="1" rid="111">
<name>sitea</name>
<lat>-31.23</lat>
<lon>151</lon>
</entry>
<entry id="2" rid="222">
<name>siteb</name>
<lat>-31.23</lat>
<lon>151</lon>
</entry>
<entry id="3" rid="333">
<name>sitec</name>
<lat>-31.23</lat>
<lon>151</lon>
</entry>
</coordReg>
<assetReg>
<entry id="11" rid="11">
<name>deva</name>
<coordRegRef fix:ridref="111"/>
<assetRegRef ridref="aa"><ridref2>aa</ridref2></assetRegRef>
</entry>
<entry id="22" rid="22">
<name>devb</name>
<coordRegRef ridref="2212"/>
<assetRegRef ridref="11"><ridref2>11</ridref2></assetRegRef>
</entry>
<entry id="33" rid="33">
<name>devc</name>
<coordRegRef ridref="333"/>
<assetRegRef ridref="44"><ridref2>44</ridref2></assetRegRef>
</entry>
</assetReg>
</assetExchange>
最佳答案
正如您所注意到的,并且从验证错误中可以看出,解析器确实需要属性 ridref但它必须在命名空间中。
原因是在 XML 模式中,所有全局元素、属性或类型定义都必须被限定。这会导致所有全局定义的属性都将以命名空间为前缀,即使您设置了 attributeFormDefault="unqualified"。 .解决方法是在 <xs:attributeGroup> 中单独定义此类属性(或全局类型)。在这种情况下,属性组的名称将获得附加到它的 namespace ,而不是其中的属性名称。然后你可以使用ref引用这些属性组并获取全局可用的属性,而无需将它们放在命名空间中。
关于xml - 如何在 xsd 中引用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025859/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2