我們有一個 XML,它需要以特定格式作為使用 xslt 的輸出。我是這個 xslt 的新手,并嘗試了多種方法,例如:
制作每個標簽的模板,并嘗試像每個模板中的一個元素一樣呼叫模板,但這并沒有發生。
Sample payload
<inputs>
<field>0</field>
<type1>1</type1>
<type2>2</type2>
<type3>3</type3>
<category1>4</category1>
<category2>5</category2>
<category3>6</category3>
</inputs>
Below is the desired output
output xml
<tag>
<type1>1</type1>
<category1>4</category1>
</tag>
<tag>
<type2>2</type2>
<category2>5</category2>
</tag>
<tag>
<type3>3</type3>
<category3>6</category3>
</tag>
uj5u.com熱心網友回復:
在給定的示例中,您可以執行以下操作:
<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:template match="/inputs">
<xsl:for-each select="*[starts-with(name(), 'type')]">
<xsl:variable name="i" select="position()" />
<tag>
<xsl:copy-of select="."/>
<xsl:copy-of select="../*[starts-with(name(), 'category')][$i]"/>
</tag>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
得到你顯示的輸出。
但是,尚不清楚該示例是否代表了需要在此處應用的實際規則。
另請注意,結果是一個 XML 片段,缺少一個根元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490150.html
下一篇:與行同名的多個節點
