dongtu1789 2019-09-20 09:03
浏览 158

使用XSD过滤anyURI类型的XML元素

I am using go-libxml2. I have an XSD and also a XML. I want to filter all anyURI elements in the XML based on XSD including nested elements. Because I am trying to update all anyURI elements in a XML by adding a queryparam. For example

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>

In the above XML, I want to filter and update all element of type "anyURI" like <AnyURI1>, <AnyURI2> and <anyURI3>

How can I achieve this using golang?

  • 写回答

1条回答 默认 最新

  • douzhuican0041 2019-09-20 12:07
    关注

    I think you should consider alternative choices of technology for this. The problem becomes very easy with a schema-aware XSLT transformation:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行