我剛剛開始學習 xslt,對此我很陌生。這就是為什么我在重試多次但沒有得到想要的輸出后在這里問這個問題。我有這個輸入 xml:
<root>
<type>sometype</type>
<attribute name="1st" value="val1"/>
<attribute name="2nd" value="val2"/>
<attribute name="nth" value="valn"/>
<attribute name="first">
<tagvalue>
<entryMap name="mapvalue1">
<new>
<type>TypeA</type>
<attribute name="attr1" value="aaa" />
</new>
</entryMap>
</tagvalue>
</attribute>
<attribute name="second">
<tagvalue>
<entryMap name="mapvalue2">
<new>
<type>TypeB</type>
<attribute name="attr2" value="bbb" />
</new>
</entryMap>
</tagvalue>
</attribute>
<attribute name="third">
<tagvalue>
<entryMap name="mapvalue3">
<new>
<type>TypeC</type>
<attribute name="attr3" value="ccc" />
</new>
</entryMap>
</tagvalue>
</attribute>
</root>
需要將其轉換為此輸出 xml:
<root>
<type>sometype</type>
<attribute name="1st" value="val1"/>
<attribute name="2nd" value="val2"/>
<attribute name="nth" value="valn"/>
<attribute name="common">
<tagvalue>
<entryMap name="mapvalue1">
<new>
<type>TypeA</type>
<attribute name="attr1" value="aaa" />
</new>
</entryMap>
</tagvalue>
<tagvalue>
<entryMap name="mapvalue2">
<new>
<type>TypeB</type>
<attribute name="attr2" value="bbb" />
</new>
</entryMap>
</tagvalue>
<tagvalue>
<entryMap name="mapvalue3">
<new>
<type>TypeC</type>
<attribute name="attr3" value="ccc" />
</new>
</entryMap>
</tagvalue>
</attribute>
</root>
也就是說,這個標簽出現了 3 次:<attribute name="first"> <attribute name="second"> <attribute name="third">。我需要在開頭只出現一次,并將名稱值重命名為“common”,即<attribute name="common">。我嘗試使用<xsl:template match="@value">重命名邏輯,但無法成功合并它。
我目前的 xslt 是這樣的:
<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="*"/>
<xsl:param name="removeElementsNamed" select="'attribute'"/>
<!-- identity transform -->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:if test="not(name() = $removeElementsNamed)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
但我知道我在某處做錯了。我嘗試使用多個模板,但沒有得到所需的輸出。如果有人可以幫助我解決這個問題,我將非常感激。謝謝!
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="*"/>
<xsl:template match="/root">
<xsl:copy>
<attribute name="common">
<xsl:copy-of select="attribute/tagvalue"/>
</attribute>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
添加:
要復制未包含在“common”組中的其他元素,您可以執行以下操作:
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="type | attribute[not(@name='first' or @name='second' or @name='third')]"/>
<attribute name="common">
<xsl:copy-of select="attribute[@name='first' or @name='second' or @name='third']/tagvalue"/>
</attribute>
</xsl:copy>
</xsl:template>
或者,更優雅一點(避免兩次測驗相同的東西):
<xsl:template match="/root">
<xsl:variable name="common" select="attribute[@name='first' or @name='second' or @name='third']" />
<xsl:copy>
<xsl:copy-of select="*[count(.|$common) != count($common)]"/>
<attribute name="common">
<xsl:copy-of select="$common/tagvalue"/>
</attribute>
</xsl:copy>
</xsl:template>
uj5u.com熱心網友回復:
這是可以做到的一種方法:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="attribute[1]">
<xsl:copy>
<xsl:attribute name="name">common</xsl:attribute>
<xsl:apply-templates select="//attribute/tagvalue"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attribute[position()>1]"/>
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
這是可以做到的一種方法:https ://xsltfiddle.liberty-development.net/nbspVbv/2
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/452391.html
下一篇:連接XML屬性SQLServer
