2017-02-27 92 views
0

<?xml version="1.0" encoding="UTF-8"?> <Root xmlns="http://www.tcxml.org/Schemas/TCXMLSchema"> <TreeNode bbd="" id="TreeNodID" vid="VirtualID" /> <ChildNode bbd="bbd1" date="2017-02-22T15:04:32Z" object="ChildNodeID" thread="TreeNodID" /> </Root>XSLT:查找另一个节点等于另一个属性节点,其具有的属性值

我想写一个XSLT将改写的XML像

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
    <TreeNode bbd="bbd1" id="TreeNodeID" vid="VirtualID" object="ChildNodeID" /> 
    <ChildNode bbd="bbd1" date=&quot;2017-02-22T15:04:32Z object="ChildNodeID" thread="TreeNodeID" /> 
</Root> 

我想找到有/*/@thread节点任何节点的属性等于值TreeNode/@id。获取匹配节点的@object属性的值,并将其填充到TreeNode元素中。还获取bbd值并将其填充到TreeNode元素中。

我不知道匹配节点是ChildNode还是别的。

我该如何做到这一点?

+0

什么是将属性从子节点移动到treenode的条件 – Rupesh

+0

了解如何使用**键**:https://www.xml.com/pub/a/2002/02/06/key-lookups.html –

回答

0

您可以使用此也

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:aa="http://www.tcxml.org/Schemas/TCXMLSchema"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="node()|@*" > 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="aa:TreeNode"> 
     <xsl:variable name="id" select="@id"/> 
     <xsl:copy> 
      <xsl:attribute name="bbd" select="//*[@thread eq $id]/@bbd"/> 
      <xsl:copy-of select="@*[normalize-space(.) ne '']"></xsl:copy-of> 
      <xsl:attribute name="object" select="//*[@thread eq $id]/@object"/> 
      <xsl:apply-templates select="node()"></xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

谢谢Rupesh。它为我工作。 – TechGuy

+0

嗨Rupesh,由于存在的属性xmlns在根节点它不会给出预期的结果... – TechGuy

+0

在xsl声明中定义该名称空间或在您的问题中提及作弊 – Rupesh

0

我可以做到这一点使用下面的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" indent="yes"/> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="TreeNode"> 
     <xsl:copy> 
      <xsl:if test="/*/node()/@[email protected]"> 
       <xsl:attribute name="object"> 
        <xsl:value-of select="/*/node()/@object"/> 
       </xsl:attribute> 
      </xsl:if>   
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

`

的剩余部分 - 如果存在BBD然后检查值然后添加属性并填充值,如果已经填充了某个值,则保持原样。 否则添加属性并填充值。

相关问题