仅当子元素尚未具有相同的属性时,才如何将属性传递给子元素?
XML:
<section>
<container attribute1="container1" attribute2="container2">
<p attribute1="test3"/>
<ol attribute2="test4"/>
<container>
<section/>
输出应如下所示:
<section>
<p attribute1="test3" attribute2="test2"/>
<ol attribute1="container1" attribute2="test4"/>
</section>
这是我尝试的:
<xsl:template match="container">
<xsl:apply-templates mode="passAttributeToChild"/>
</xsl:template>
<xsl:template match="*" mode="passAttributeToChildren">
<xsl:element name="{name()}">
<xsl:for-each select="@*">
<xsl:choose>
<xsl:when test="name() = name(../@*)"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="*|text()"/>
</xsl:element>
</xsl:template>
任何帮助将不胜感激;)提前谢谢!
多次声明的属性会相互覆盖,因此很容易。
<xsl:template match="container/*">
<xsl:copy>
<xsl:copy-of select="../@*" /> <!-- take default from parent -->
<xsl:copy-of select="@*" /> <!-- overwrite if applicable -->
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
如您的样本所示,这假定您需要所有父级属性。当然,您可以决定要继承哪些属性:
<xsl:copy-of select="../@attribute1 | ../@attribute2" />
<xsl:copy-of select="@attribute1 | @attribute2">
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句