XSLTを初めて使用するので、2番目の親ノードから始まるすべての親ノードに同じ属性と値を追加したいと思います。ここでのロジックは、メインノードがある場合、属性(Mainattribute)は1回であり、メインノードの下の残りのすべての親ノードは、メインノード以外のすべてで同じである異なる属性(childattribute)を持つ必要があります。
以下のような入力xmlがあります:いくつかのフィールドのみがあり、理想的にはより多くのタグがあり、異なる場合があります。
<?xml version="1.0" encoding="UTF-8"?>
<Header>
<Main>
<Parent2>
<status>12</status>
<statusmsg>Helo</status_text>
</Parent2>
<Parent3>
<Child1>112</Child1>
<Child2>Hai</Child2>
</Parent3>
<Parent4>
<Child3>Valley</Child3>
<Parent5>
<Child7>Kind</Child1>
<Child8>Pls</Child2>
</Parent5>
</Parent4>
</Main>
</Header>
出力は次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<Header>
<Main Mainattribute="1">
<Parent2 childattribute="1">
<status>12</status>
<statusmsg>Helo</status_text>
</Parent2>
<Parent3 childattribute="1">
<Child1>112</Child1>
<Child2>Hai</Child2>
</Parent3>
<Parent4 childattribute="1">
<Child3>Valley</Child3>
<Parent5 childattribute="1">
<Child7>Kind</Child1>
<Child8>Pls</Child2>
</Parent5>
</Parent4>
</Main>
</Header>
誰かが同じためにXSLTを共有できますか?私は非常に多くのケースを試しましたが、それを達成することができませんでした。ありがとうございました
以下は、XSLTが最初のメインノードに対して試行したものですが、どういうわけかエラーが発生し、それ以上続行できませんでした。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!-- Template to copy all the elements -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Main">
<Main>
<xsl:attribute name="Mainattribute">
<xsl:value-of select="1"/>
</xsl:attribute>
<xsl:apply-templates select="child::node()"/>
</Main>
</xsl:template>
</xsl:stylesheet>
@Aniket Vの回答を強化すると、タグの名前に依存する代わりに、モードに頼ることができます。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Main">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates mode="parent_mode"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="parent_mode">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
更新
子を持つすべてのXML要素(最上位要素ではない)を更新する必要がある場合、この変換はあなたのものです:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Main" priority="1">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[child::* and ancestor::*]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加