gpt4 book ai didi

xml - XSLT - 在展平 xml 后组合多个属性(连接)

转载 作者:数据小太阳 更新时间:2023-10-29 02:33:51 28 4
gpt4 key购买 nike

我有一个要展平的简单 xml(元素到属性)

输入xml

<?xml version="1.0" ?>
<queryResponse type="Clients" rootUrl="https://10.130.0.192/webacs/api/v1/data" requestUrl="https://10.30.0.192/webacs/api/v1/data/Clients?.full=true&amp;.firstResult=0&amp;.maxResults=1000" responseType="listEntityInstances" count="349" first="0" last="348">
<entity url="https://10.130.0.192/webacs/api/v1/data/Clients/621621" type="Clients" dtoType="clientsDTO">
<clientsDTO id="621621" displayName="621621">
<associationTime>1474746255456</associationTime>
<clientInterface>GigabitEthernet1/0/16</clientInterface>
<connectionType>WIRED</connectionType>
<deviceIpAddress>10.1.1.254</deviceIpAddress>
<deviceName>stack</deviceName>
<deviceType>Unknown</deviceType>
<ipAddress>10.30.0.106</ipAddress>
<location>Unknown</location>
<macAddress>macaddress</macAddress>
<protocol>DOT3</protocol>
<securityPolicyStatus>PASSED</securityPolicyStatus>
<status>ASSOCIATED</status>
<updateTime>1474746256758</updateTime>
<vendor>Netapp</vendor>
<vlan>vlan_300_server</vlan>
<vlanId>300</vlanId>
</clientsDTO>
</entity>
</queryResponse>

xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="queryResponse">
<Data>
<xsl:apply-templates/>
</Data>
</xsl:template>

<xsl:template match="clientsDTO">
<Client>
<xsl:for-each select="descendant::*[not(*)]">
<xsl:attribute name="{name()}">
<xsl:value-of select="text()"/>
</xsl:attribute>

</xsl:for-each>
</Client>
</xsl:template>
</xsl:stylesheet>

它工作正常并为我提供了扁平化的输出 xml。

现在有了这个 XSLT,我想再添加一件事。元素 clientInterface 和 deviceName 需要(另外)连接为一个新属性。

关于如何实现这一点有什么想法吗?

更新:

我当前的输出 xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Client associationTime="1474746255456" clientInterface="GigabitEthernet1/0/16" connectionType="WIRED" deviceIpAddress="10.1.1.254" deviceName="stack" deviceType="Unknown" ipAddress="10.130.0.106" location="Unknown" macAddress="macaddress" protocol="DOT3" securityPolicyStatus="PASSED" status="ASSOCIATED" updateTime="1474746256758" vendor="Netapp" vlan="vlan_300_server" vlanId="300" />
</Data>

现在在那个“我想有一个新的属性,它连接 deviceName 和 clientInterface 导致:

<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Client **deviceId="stack_GigabitEthernet1/0/16"**associationTime="1474746255456" clientInterface="GigabitEthernet1/0/16" connectionType="WIRED" deviceIpAddress="10.1.1.254" deviceName="stack" deviceType="Unknown" ipAddress="10.130.0.106" location="Unknown" macAddress="macaddress" protocol="DOT3" securityPolicyStatus="PASSED" status="ASSOCIATED" updateTime="1474746256758" vendor="Netapp" vlan="vlan_300_server" vlanId="300" />
</Data>

最佳答案

你可以这样做:

<xsl:template match="clientsDTO">
<Client>
<xsl:attribute name="deviceId">
<xsl:value-of select="deviceName"/>
<xsl:text>_</xsl:text>
<xsl:value-of select="clientInterface"/>
</xsl:attribute>
<xsl:for-each select="*[not(*)]">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
</Client>
</xsl:template>

注意:使用 descendant 轴效率低下 - 在您的情况下也没有必要。

关于xml - XSLT - 在展平 xml 后组合多个属性(连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39957522/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com