以下是我的XML
<document>
<businessobjects>
<AssetID>
<Code>002515</Code>
<RelatedAsset>
<AssetID>
<Code>A000507</Code>
</AssetID>
</RelatedAsset>
<DependentAsset>
<AssetID>
<Code>002519</Code>
</AssetID>
</DependentAsset>
</AssetID>
<AssetID>
<Code>002516</Code>
<DependentAsset>
<AssetID>
<Code>002515</Code>
<RelatedAsset>
<AssetID>
<Code>A000507</Code>
</AssetID>
</RelatedAsset>
<DependentAsset>
<AssetID>
<Code>002519</Code>
</AssetID>
</DependentAsset>
</AssetID>
</DependentAsset>
</AssetID>
<AssetID>
<Code>002519</Code>
</AssetID>
</businessobjects>
</document>
我希望将其转换为以下格式
<?xml version="1.0" encoding="UTF-8"?>
<document>
<businessobjects>
<Assets>
<Code>002515</Code>
<Parent_Asset>****</Parent_Asset>
<Dependent_Asset>002519</Dependent_Asset>
<Related_Asset>A000507</Related_Asset>
</Assets>
<Assets>
<Code>002516</Code>
<Parent_Asset>*****</Parent_Asset>
<Dependent_Asset>002515</Dependent_Asset>
<Related_Asset/>
</Assets>
<Assets>
<Code>002519</Code>
<Parent_Asset>#####</Parent_Asset>
<Dependent_Asset/>
<Related_Asset/>
</Assets>
</businessobjects>
</document>
除了&#34; Parent_Asset&#34;节点之外,我能够获得其他元素。 对于002519,Parent_Asset应为002515,对于002515,Parent_Asset应为002516
以下是我尝试过的XSLT。
<?xml version="1.0" encoding="utf-8"?>
<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="/document">
<document>
<businessobjects>
<xsl:for-each select="businessobjects/AssetID">
<Assets>
<Code><xsl:value-of select="child::Code[1]"/></Code>
<Parent_Asset>
</Parent_Asset>
<Dependent_Asset><xsl:value-of select="DependentAsset/AssetID/Code"/></Dependent_Asset>
<Related_Asset><xsl:value-of select="RelatedAsset/AssetID/Code"/></Related_Asset>
</Assets>
</xsl:for-each>
</businessobjects>
</document>
</xsl:template>
</xsl:stylesheet>
请帮忙。什么是获得父资产的逻辑
答案 0 :(得分:1)
您可以创建密钥以按代码
查找DependentAsset
个元素
<xsl:key name="Dependent" match="businessobjects/AssetID/DependentAsset"
use="AssetID/Code" />
然后您可以像这样计算Parent_Asset
值:
<Parent_Asset>
<xsl:value-of select="key('Dependent', Code)/../Code" />
</Parent_Asset>
因此,密钥获取引用当前Dependent
的{{1}},并使用Code
获取包含../Code
的代码。