【问题标题】:Filter XML elements of type anyURI using XSD使用 XSD 过滤 anyURI 类型的 XML 元素
【发布时间】:2019-09-20 09:03:32
【问题描述】:

我正在使用go-libxml2。我有一个 XSD 和一个 XML。我想基于 XSD 过滤 XML 中的所有 anyURI 元素,包括嵌套元素。因为我试图通过添加查询参数来更新 XML 中的所有 anyURI 元素。例如

XSD:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="grandParent">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="AnyURI1" type="xs:anyURI" 
         maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="parent">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="AnyURI2" type="xs:anyURI" 
           maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
    <xs:element name="children">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="AnyURI3" type="xs:anyURI" 
             maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:element>
</xs:element>
</xs:schema>

XML

    <grandParent>
  <AnyURI1>    http://uri_gp1.com </AnyURI1>
  <AnyURI1>    http://uri_gp2.com </AnyURI1>
  <parent>
    <AnyURI2>    http://uri_p1.com    </AnyURI2>
    <AnyURI2>    http://uri_p2.com    </AnyURI2>
    <children>
      <AnyURI3>    http://uri_child1.com    </AnyURI3>
      <AnyURI3>    http://uri_child2.com    </AnyURI3>
    </children>
  </parent>
</grandParent>

在上面的 XML 中,我想过滤和更新所有类型为“anyURI”的元素,例如 &lt;AnyURI1&gt;, &lt;AnyURI2&gt; and &lt;anyURI3&gt;

如何使用 golang 实现这一点?

【问题讨论】:

    标签: xml go xsd libxml2


    【解决方案1】:

    我认为您应该为此考虑其他技术选择。使用模式感知 XSLT 转换问题变得非常简单:

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
                   xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xsl:mode on-no-match="shallow-copy">
      <xsl:template match="element(*, xs:anyURI)/text()">
        <xsl:value-of select=". || '?param=value'"/>
      </xsl:template>
    </xsl:transform>
    

    【讨论】:

    • 感谢您的建议。是个好主意。但我没有看到任何好的库可以在 golang 中实现这一点。我还在等待更多的 cmets。
    【解决方案2】:

    对于支持 XML Schema 的全功能 XML 处理器,Post Schema Validation Infoset (PSVI) 将为您提供所需的一切。 PSVI Example 展示了如何使用 Xerces C 访问它。

    据我所知,go-libxml2 的问题在于 libxml2 本身并没有实现 PSVI。设计师允许我认为从未实现的“未来”PSVI 扩展。 (查看libxml2 doc 并搜索 PSVI。)因此,答案是您无法实现您想要的。

    如果您愿意与其他平台集成,Java xerces 可以工作,您可以将任务委托给它。

    【讨论】:

      猜你喜欢
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多