我對 xslt 很陌生,經過多次重試后,我無法從這里繼續。我準備的當前xslt是:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="action">
<action>
<xsl:apply-templates select="@*|node()"/>
<attribute name="phase" value="new" />
</action>
</xsl:template>
</xsl:stylesheet>
這會更改此輸入 xml:
<root>
<attribute name="phases">
<tag>
<entryMap name="startphase">
<action>
<type>Some type</type>
<attribute name="first" value="abc" />
</action>
</entryMap>
</tag>
</attribute>
</root>
到此輸出 xml(<attribute name="phase" value="new"/>在內部<action>和之后添加<type>):
<root>
<attribute name="phases">
<tag>
<entryMap name="startphase">
<action>
<type>Some type</type>
<attribute name="first" value="abc"/>
<attribute name="phase" value="new"/>
</action>
</entryMap>
</tag>
</attribute>
</root>
我的 xml 檔案可以更改并具有多個階段,因此我需要在 xslt 檔案中進行更改以轉換此示例輸入 xml:
<root>
<attribute name="phases">
<tag>
<entryMap name="startphase">
<action>
<type>Some type</type>
<attribute name="first" value="abc" />
</action>
</entryMap>
</tag>
</attribute>
<attribute name="phases">
<tag>
<entryMap name="midphase">
<action>
<type>Some other type</type>
<attribute name="second" value="def" />
</action>
</entryMap>
</tag>
</attribute>
<attribute name="phases">
<tag>
<entryMap name="endphase">
<action>
<type>Other type</type>
<attribute name="third" value="ghi" />
</action>
</entryMap>
</tag>
</attribute>
</root>
到這個輸出檔案:
<root>
<attribute name="phases">
<tag>
<entryMap name="startphase">
<action>
<type>Some type</type>
<attribute name="first" value="abc"/>
<attribute name="phase" value="startphase"/>
</action>
</entryMap>
</tag>
</attribute>
<attribute name="phases">
<tag>
<entryMap name="midphase">
<action>
<type>Some other type</type>
<attribute name="second" value="def"/>
<attribute name="phase" value="midphase"/>
</action>
</entryMap>
</tag>
</attribute>
<attribute name="phases">
<tag>
<entryMap name="endphase">
<action>
<type>Other type</type>
<attribute name="third" value="ghi"/>
<attribute name="phase" value="endphase"/>
</action>
</entryMap>
</tag>
</attribute>
</root>
也就是說,我需要添加<attribute name="phase" value="entryMap name value"/>到每個階段,并且 value 屬性應該包含 entryMap name 屬性值的值。
我已經嘗試并瀏覽了許多鏈接和教程,但我仍然無法實作這一點。如果有人可以幫助我,我將非常非常感激。先感謝您!
uj5u.com熱心網友回復:
怎么樣:
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="action">
<xsl:copy>
<xsl:copy-of select="*"/>
<attribute name="phase" value="{../@name}"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
要了解其作業原理,請閱讀屬性值模板和縮寫語法。
添加:
有什么方法可以在標簽
<attribute name="phase" value="entryMap name value"/>之后添加?<type></type>
你可以這樣做:
<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="type">
<xsl:copy-of select="."/>
<attribute name="phase" value="{../../@name}"/>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452394.html
