【发布时间】:2019-05-21 01:16:27
【问题描述】:
大家好,感谢您的帮助。 我必须转换父节点和他的所有子节点,但我不太清楚 XSLT 是如何做的。
选择
- 包含子节点
的节点在包含子节点 的节点之前; -
节点根据
和 的 "AddrStart" 属性按升序排序> 的“AddrBase” 属性。
我有这个 XML 代码:
<NetworkAliases>
<NetAlias UID="{02A4738B-605C-4641-9705-E83ADFDAB221}" ZoneType="1" Name="comodo">
<AddressItems>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="114.255.52.160" AddrEnd="114.255.52.175"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="123.124.255.96" AddrEnd="123.124.255.111"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="69.195.46.36" AddrEnd="255.255.6.39"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="97.107.169.84" AddrEnd="97.107.169.87"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="97.107.175.140" AddrEnd="97.107.175.143"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="199.66.200.0" AddrEnd="199.66.207.255"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="208.49.40.28" AddrEnd="208.49.40.31"/>
</Address>
<Address Source="2" Type="2">
<IPV6 AddrType="4" AddrMask="48" AddrBase="2607F7A80E0A00000000000000000000"/>
</Address>
<Address Source="2" Type="2">
<IPV6 AddrType="4" AddrMask="48" AddrBase="2607F7A8100900000000000000000000"/>
</Address>
</AddressItems>
</NetAlias>
<NetAlias UID="{0B9F7F4D-E23B-4D09-88F8-0CA378319316}" ZoneType="1" Name="akamai">
<AddressItems>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="2.22.80.0" AddrEnd="2.22.95.255"/>
</Address>
<Address Source="2" Type="2">
<IPV6 AddrType="4" AddrMask="48" AddrBase="2A0226F000DF00000000000000000000"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="95.100.224.0" AddrEnd="95.100.239.255"/>
</Address>
</AddressItems>
</NetAlias>
</NetworkAliases>
这个 xsl(适用于其他节点,但不适用于这部分):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="PolicyItems">
<xsl:copy>
<xsl:apply-templates select="PolicyItem">
<xsl:sort select="@Filename" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Predefined">
<xsl:copy>
<xsl:apply-templates select="PredefinedItem">
<xsl:sort select="@Name" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="NetworkAliases">
<xsl:copy>
<xsl:apply-templates select="NetAlias">
<xsl:sort select="@Name" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="AddressItems">
<xsl:copy>
<xsl:apply-templates select="Address">
<xsl:sort select="@Type" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="PortAliases">
<xsl:copy>
<xsl:apply-templates select="PortAlias">
<xsl:sort select="@Name" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- To Sort also the ports -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果应该是:
<NetworkAliases>
<NetAlias UID="{0B9F7F4D-E23B-4D09-88F8-0CA378319316}" ZoneType="1" Name="akamai">
<AddressItems>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="2.22.80.0" AddrEnd="2.22.95.255"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="95.100.224.0" AddrEnd="95.100.239.255"/>
</Address>
<Address Source="2" Type="2">
<IPV6 AddrType="4" AddrMask="48" AddrBase="2A0226F000DF00000000000000000000"/>
</Address>
</AddressItems>
</NetAlias>
<NetAlias UID="{02A4738B-605C-4641-9705-E83ADFDAB221}" ZoneType="1" Name="comodo">
<AddressItems>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="69.195.46.36" AddrEnd="255.255.6.39"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="97.107.169.84" AddrEnd="97.107.169.87"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="97.107.175.140" AddrEnd="97.107.175.143"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="114.255.52.160" AddrEnd="114.255.52.175"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="123.124.255.96" AddrEnd="123.124.255.111"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="199.66.200.0" AddrEnd="199.66.207.255"/>
</Address>
<Address Source="2" Type="1">
<IPV4 AddrType="2" AddrStart="208.49.40.28" AddrEnd="208.49.40.31"/>
</Address>
<Address Source="2" Type="2">
<IPV6 AddrType="4" AddrMask="48" AddrBase="2607F7A80E0A00000000000000000000"/>
</Address>
<Address Source="2" Type="2">
<IPV6 AddrType="4" AddrMask="48" AddrBase="2607F7A8100900000000000000000000"/>
</Address>
</AddressItems>
</NetAlias>
</NetworkAliases>
我试了很多次都没有结果,最后我不得不问你。谢谢
【问题讨论】: