我有一个独特的XML文件,我试图使用XSL文件进行转换,以支持由另一个应用程序导入。我是一个非常新的XML转换和短时间线,所以请原谅我,如果我没有用尽google最后一个网站返回最理想的方式来做到这一点。
原始XML(最初从JSON转换而来)
<?xml version="1.0" encoding="UTF-8" ?>
<SecurityGroups>
<Description>DEV-CUSTOMER-VPC-AD</Description>
<Tags>
<Value>DEV-CUSTOMER-VPC-AD</Value>
<Key>Name</Key>
</Tags>
<IpPermissions>
<FromPort>42424</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>42424</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>464</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>464</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>4500</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>4500</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>389</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>389</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>53</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>53</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>3389</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>3389</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>445</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>445</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>1813</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>1813</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>1900</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>1900</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>139</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>139</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>135</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>135</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>53</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>53</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>500</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>500</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>52997</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>57000</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>80</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>80</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>464</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>464</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>593</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>593</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>389</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>389</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>123</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>123</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>443</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>443</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>9389</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>9389</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>3268</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>3269</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>88</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>88</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>52997</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>57000</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>137</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>138</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>1812</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>1812</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>2869</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>2869</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>636</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>636</ToPort>
<IpProtocol>tcp</IpProtocol>
</IpPermissions>
<IpPermissions>
<FromPort>88</FromPort>
<IpRanges>
<CidrIp>10.0.0.0/8</CidrIp>
</IpRanges>
<ToPort>88</ToPort>
<IpProtocol>udp</IpProtocol>
</IpPermissions>
<GroupName>DEV-CUSTOMER-VPC-AD</GroupName>
<VpcId>vpc-a2a754c7</VpcId>
<OwnerId>006895185235</OwnerId>
<GroupId>sg-27c51342</GroupId>
</SecurityGroups>
到目前为止,我有以下XSL文件:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/SecurityGroups">
<SecurityGroups>
<Description><xsl:value-of select="Description" /></Description>
<Name><xsl:value-of select="GroupName" /></Name>
<xsl:value-of select="."/>
<xsl:if test="following::FromPort">;</xsl:if>
</SecurityGroups>
</xsl:template>
</xsl:stylesheet>
我希望FromPort整数显示为逗号分隔,如下面的XML。
<?xml version="1.0" encoding="utf-8"?>
<Export type="PortList" date="January 25, 2017 19:03" version="9.6.3177">
<PortLists>
<PortList id="119">
<TBUID></TBUID>
<Name>Active Directory Domain Services</Name>
<Description>Mainly for Active Directory Servers North of 2k8 to 2k12</Description>
<Items>88,1812,137-138,123,445,500,1813,1900,52997 - 57000,53,389,464</Items>
<Version></Version>
<UserEdited>true</UserEdited>
</PortList>
</PortLists>
</Export>
如果有人认出其中的一部分我试图解析AWS安全组以自动导入到TrendMicro DSM防火墙规则中。
非常感谢任何帮助。
答案 0 :(得分:0)
您的问题相当令人困惑,因为您显示的输出似乎不是您想要的确切输出。
以此为出发点:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/SecurityGroups">
<SecurityGroups>
<Description>
<xsl:value-of select="Description" />
</Description>
<Name>
<xsl:value-of select="GroupName" />
</Name>
<Items>
<xsl:for-each select="IpPermissions">
<xsl:value-of select="FromPort"/>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
</Items>
</SecurityGroups>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<SecurityGroups>
<Description>DEV-CUSTOMER-VPC-AD</Description>
<Name>DEV-CUSTOMER-VPC-AD</Name>
<Items>42424,464,4500,389,53,3389,445,1813,1900,139,135,53,500,52997,80,464,593,389,123,443,9389,3268,88,52997,137,1812,2869,636,88</Items>
</SecurityGroups>