XSLT:从骨架向节点添加名称空间

时间:2017-01-27 21:54:48

标签: ruby xml xslt nokogiri

我有一个XML文档,目前在节点级别缺少名称空间。我希望能够复制骨架文档中定义的名称空间。

我正在使用Nokogiri(Ruby)处理文档,并且需要XSLT 1.0或任何其他可行的方法。

这甚至可能吗?

文档

  <routing
  xmlns:rt="urn:ietf:params:xml:ns:yang:ietf-routing"
  xmlns:cisco-ospf="urn:ietf:params:xml:ns:yang:cisco-ospf"
  xmlns:ospf-devs="http://cisco.com/ns/ietf-ospf/devs"
  xmlns:ospf="urn:ietf:params:xml:ns:yang:ietf-ospf"
  xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
  <routing-instance>
      <name>default</name>
      <description>default-vrf [read-only]</description>
      <routing-protocols>
          <routing-protocol>
              <type>ospf:ospfv2</type>
              <name>100</name>
              <ospf>
                  <instance>
                      <af>rt:ipv4</af>
                      <router-id>6.6.6.6</router-id>
                      <admin-distance>
                          <intra-area>100</intra-area>
                          <inter-area>200</inter-area>
                          <external>10</external>
                      </admin-distance>
                      <auto-cost>
                          <enable>true</enable>
                          <reference-bandwidth>5000</reference-bandwidth>
                      </auto-cost>
                      <area>
                        <area-id>50</area-id>
                        <default-cost>50</default-cost>
                      </area>
                      <area>
                        <area-id>100</area-id>
                        <default-cost>100</default-cost>
                      </area>
                      <area>
                        <area-id>300</area-id>
                        <virtual-link>
                          <router-id>8.8.8.8</router-id>
                          <hello-interval>80</hello-interval>
                        </virtual-link>
                      </area>
                      <area>
                        <area-id>666</area-id>
                        <default-cost>100</default-cost>
                      </area>
                      <redistribution>
                          <static>
                              <redist-option>
                                  <metric>99999</metric>
                                  <metric-type>1</metric-type>
                              </redist-option>
                          </static>
                      </redistribution>
                  </instance>
              </ospf>
          </routing-protocol>
      </routing-protocols>
  </routing-instance>

骨架

  <routing
  xmlns:rt="urn:ietf:params:xml:ns:yang:ietf-routing"
  xmlns:cisco-ospf="urn:ietf:params:xml:ns:yang:cisco-ospf"
  xmlns:ospf-devs="http://cisco.com/ns/ietf-ospf/devs"
  xmlns:ospf="urn:ietf:params:xml:ns:yang:ietf-ospf"
  xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
  <routing-instance xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
    <name xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
    <router-id xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
    <description xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
        <interface xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
      </interfaces>
      <routing-protocols xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
          <routing-protocol xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
            <name xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
            <static-routes xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
              <ospf xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                  <instance xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                    <af xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                    <router-id xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                      <admin-distance xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                        <intra-area xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                        <inter-area xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                        <external xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                      </admin-distance>
                      <nsr xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                        <enable xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                      </nsr>
                      <auto-cost xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                        <enable xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                        <reference-bandwidth xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                      </auto-cost>
                      <area xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                        <area-id xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                        <default-cost xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf"/>
                      </area>
                      <default-metric xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf"/>
                      <redistribution xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf">
                          <static xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf">
                              <redist-option xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf">
                                <metric xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf"/>
                                <metric-type xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf"/>
                              </redist-option>
                          </static>
                      </redistribution>
                  </instance>
              </ospf>
          </routing-protocol>
      </routing-protocols>
  </routing-instance>

所需的输出

  <routing
  xmlns:rt="urn:ietf:params:xml:ns:yang:ietf-routing"
  xmlns:cisco-ospf="urn:ietf:params:xml:ns:yang:cisco-ospf"
  xmlns:ospf-devs="http://cisco.com/ns/ietf-ospf/devs"
  xmlns:ospf="urn:ietf:params:xml:ns:yang:ietf-ospf"
  xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
  <routing-instance xmlns="urn:ietf:params:xml:ns:yang:ietf-routing">
      <name>default</name>
      <description>default-vrf [read-only]</description>
      <routing-protocols>
          <routing-protocol>
              <type>ospf:ospfv2</type>
              <name>100</name>
              <ospf xmlns="urn:ietf:params:xml:ns:yang:ietf-ospf">
                  <instance>
                      <af>rt:ipv4</af>
                      <router-id>6.6.6.6</router-id>
                      <admin-distance>
                          <intra-area>100</intra-area>
                          <inter-area>200</inter-area>
                          <external>10</external>
                      </admin-distance>
                      <auto-cost>
                          <enable>true</enable>
                          <reference-bandwidth>5000</reference-bandwidth>
                      </auto-cost>
                      <area>
                        <area-id>50</area-id>
                        <default-cost>50</default-cost>
                      </area>
                      <area>
                        <area-id>100</area-id>
                        <default-cost>100</default-cost>
                      </area>
                      <area>
                        <area-id>300</area-id>
                        <virtual-link>
                          <router-id>8.8.8.8</router-id>
                          <hello-interval>80</hello-interval>
                        </virtual-link>
                      </area>
                      <area>
                        <area-id>666</area-id>
                        <default-cost>100</default-cost>
                      </area>
                      <redistribution xmlns="urn:ietf:params:xml:ns:yang:cisco-ospf">
                          <static>
                              <redist-option>
                                  <metric>99999</metric>
                                  <metric-type>1</metric-type>
                              </redist-option>
                          </static>
                      </redistribution>
                  </instance>
              </ospf>
          </routing-protocol>
      </routing-protocols>
  </routing-instance>

1 个答案:

答案 0 :(得分:0)

您想要的文档包含许多冗余命名空间声明。

无法强制XSLT序列化程序输出冗余的名称空间声明。

那是因为很难看出为什么有人会想要它们。

如果您的真正目标是根据某些映射文件定义更改元素的命名空间,而不是输出表示这些命名空间的冗余命名空间声明,那么最好更改示例。