使用XSLT

时间:2016-12-06 23:02:23

标签: xml xslt

我正在研究Ubuntu 14.04 LTS,我需要解析以下XML文件:

<?xml version="1.0"?>
<!DOCTYPE network SYSTEM "network.dtd">
<network locale="C" version="1.7" attribute_processing="explicit">
<subnet name="subnet_0">
     <attr name="priority" value="0"/>
    <attr name="user id" value="0"/>
    <attr name="x position" value="73.36"/>
    <attr name="y position" value="22.61"/>
    <attr name="outline color" value="blue"/>
    <attr name="creation source" value="Object Palette"/>
    <attr name="creation timestamp" value="16:50:54 Dec 01 2016"/>
    <attr name="creation data" value=""/>
    <characteristic name="units" value="Logical Units"/>
    <view-props>
        <attr name="map" value="world"/>
        <attr name="slice.count" value="0"/>
        <attr name="grid division" value="2e+100"/>
        <attr name="grid style" value="disabled"/>
        <attr name="grid color" value="#808080"/>
        <attr name="resolution" value="2e+100"/>
        <attr name="ul x" value="-50"/>
        <attr name="ul y" value="50"/>
        <attr name="lr x" value="50"/>
        <attr name="lr y" value="-50"/>
        <attr name="iso map names.count" value="0"/>
        <attr name="current view" value="Default View"/>
        <attr name="view mode" value="geographic"/>
    </view-props>
</subnet>
<subnet name="subnet_1">
    <attr name="priority" value="0"/>
    <attr name="user id" value="0"/>
    <attr name="x position" value="75.37"/>
    <attr name="y position" value="19.88"/>
    <attr name="outline color" value="blue"/>
    <attr name="creation source" value="Object Palette"/>
    <attr name="creation timestamp" value="16:50:55 Dec 01 2016"/>
    <attr name="creation data" value=""/>
    <characteristic name="units" value="Logical Units"/>
    <view-props>
        <attr name="map" value="world"/>
        <attr name="slice.count" value="0"/>
        <attr name="grid division" value="2e+100"/>
        <attr name="grid style" value="disabled"/>
        <attr name="grid color" value="#808080"/>
        <attr name="resolution" value="2e+100"/>
        <attr name="ul x" value="-50"/>
        <attr name="ul y" value="50"/>
        <attr name="lr x" value="50"/>
        <attr name="lr y" value="-50"/>
        <attr name="iso map names.count" value="0"/>
        <attr name="current view" value="Default View"/>
        <attr name="view mode" value="geographic"/>
    </view-props>
</subnet>
<subnet name="subnet_2">
    <attr name="priority" value="0"/>
    <attr name="user id" value="0"/>
    <attr name="x position" value="77.42"/>
    <attr name="y position" value="16.63"/>
    <attr name="outline color" value="blue"/>
    <attr name="creation source" value="Object Palette"/>
    <attr name="creation timestamp" value="16:50:55 Dec 01 2016"/>
    <attr name="creation data" value=""/>
    <characteristic name="units" value="Logical Units"/>
    <view-props>
        <attr name="map" value="world"/>
        <attr name="slice.count" value="0"/>
        <attr name="grid division" value="2e+100"/>
        <attr name="grid style" value="disabled"/>
        <attr name="grid color" value="#808080"/>
        <attr name="resolution" value="2e+100"/>
        <attr name="ul x" value="-50"/>
        <attr name="ul y" value="50"/>
        <attr name="lr x" value="50"/>
        <attr name="lr y" value="-50"/>
        <attr name="iso map names.count" value="0"/>
        <attr name="current view" value="Default View"/>
        <attr name="view mode" value="geographic"/>
    </view-props>
</subnet>
<attr name="x position" value="0.0"/>
<attr name="y position" value="0.0"/>
<attr name="creation source" value="Object Palette"/>
<attr name="creation timestamp" value="Unknown"/>
<attr name="creation data" value=""/>
<characteristic name="units" value="Degrees"/>
<view-props>
    <attr name="map" value="world"/>
    <attr name="slice.count" value="9"/>
    <attr name="slice.type [0]" value="mif map"/>
    <attr name="slice.ui_status [0]" value="visible"/>
    <attr name="slice.name [0]" value="world"/>
    <attr name="slice.x [0]" value="0.000000"/>
    <attr name="slice.y [0]" value="0.000000"/>
    <attr name="slice.type [1]" value="shapeset"/>
    <attr name="slice.ui_status [1]" value="visible"/>
    <attr name="slice.name [1]" value="DefaultOcean"/>
    <attr name="slice.x [1]" value="0.000000"/>
    <attr name="slice.y [1]" value="0.000000"/>
    <attr name="slice.type [2]" value="shapeset"/>
    <attr name="slice.ui_status [2]" value="visible"/>
    <attr name="slice.name [2]" value="DefaultWorld"/>
    <attr name="slice.x [2]" value="0.000000"/>
    <attr name="slice.y [2]" value="0.000000"/>
    <attr name="slice.type [3]" value="background objects"/>
    <attr name="slice.ui_status [3]" value="visible"/>
    <attr name="slice.name [3]" value=""/>
    <attr name="slice.type [4]" value="border map"/>
    <attr name="slice.ui_status [4]" value="visible"/>
    <attr name="slice.name [4]" value=""/>
    <attr name="slice.type [5]" value="grid"/>
    <attr name="slice.ui_status [5]" value="visible"/>
    <attr name="slice.name [5]" value=""/>
    <attr name="slice.type [6]" value="shapeset"/>
    <attr name="slice.ui_status [6]" value="visible"/>
    <attr name="slice.name [6]" value="DefaultCities"/>
    <attr name="slice.x [6]" value="0.000000"/>
    <attr name="slice.y [6]" value="0.000000"/>
    <attr name="slice.type [7]" value="orbits"/>
    <attr name="slice.ui_status [7]" value="visible"/>
    <attr name="slice.name [7]" value=""/>
    <attr name="slice.type [8]" value="network objects"/>
    <attr name="slice.ui_status [8]" value="visible"/>
    <attr name="slice.name [8]" value=""/>
    <attr name="grid division" value="2e+100"/>
    <attr name="grid style" value="dashed"/>
    <attr name="grid color" value="#808080"/>
    <attr name="resolution" value="21.8902"/>
    <attr name="ul x" value="35.53"/>
    <attr name="ul y" value="35.7749"/>
    <attr name="lr x" value="97.5669"/>
    <attr name="lr y" value="-5.11096"/>
    <attr name="iso map names.count" value="0"/>
    <attr name="current view" value="Default View"/>
    <attr name="view mode" value="geographic"/>
</view-props>
</network>

此XML来自网络模拟工具(OPNET)。

我需要解析以下字段:

<subnet name="subnet_0">
<attr name="x position" value="75.37"/>
<attr name="y position" value="19.88"/>

我需要为<subnet_name=>

的每个实例迭代这个

从这些字段中我只需要以下内容:

  • subnet_0 75.37 19.88。这些字段需要用空格分隔。

然后我需要添加以下字段:

  • COMPONENT&lt; - new field
  • {}&lt; - new field
  • 0&lt; - new field
  • 75.37&lt; - 来自XML
  • 19.88&lt; - 来自XML
  • 0&lt; - new field
  • 180&lt; - new field
  • 360&lt; - new field
  • 3000&lt; - new field
  • HOSTNAME = subnet_0&lt; - 来自XML,开头添加了“HOSTNAME =”。

对于每个子网实例

,最终输出应该是一个单独的行

对于此XML示例,输出应如下所示:

COMPONENT   {}  0   73.36   22.61   0   180 360 3000    HOSTNAME=subnet_0

COMPONENT   {}  0   75.37   19.88   0   180 360 3000    HOSTNAME=subnet_1

COMPONENT   {}  0   77.42   16.63   0   180 360 3000    HOSTNAME=subnet_2

请帮助使用XSLT脚本。

0 个答案:

没有答案
相关问题