[英]Extract attribute value of parent node using php along with corresponding text values of child nodes
[英]How move Child Nodes along with values to grand parent node
我嘗試了幾種不同的方式將所有子節點都轉移到父級父節點,模式匹配只發生在一個子節點上,而不是遞歸的。 在這里感謝任何幫助。
<grandparent1>
<parent1>
<child1>1</child1>
<child2>2</child2>
</parent2>
</grandparent1>
應該成為
<grandparent1>
<child1>1</child1>
<child2>2</child2>
</grandparent1>
子節點的大小各不相同。 感謝這里的任何幫助
使用XSLT的遞歸處理模型非常容易做到這一點。
首先,使用身份轉換模板按原樣遞歸復制所有內容 。 然后為父節點添加一個例外,並使它們繼續遞歸而不復制自己。
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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="parent1 | parent2">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
要對任何父節點(即第二級)節點使用通用名稱,而無需事先知道其名稱,可以使用:
<xsl:template match="/*/*">
<xsl:apply-templates/>
</xsl:template>
作為第二個模板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.