我有以下格式的 XML
<Envelope>
<Body>
<Ref id="ref1">
<user>Test1</user>
<company>Comp1</company>
<message href="#ref3" />
</Ref>
<Ref id="ref2">
<user>Test2</user>
<company>Comp2</company>
<message href="#ref4" />
</Ref>
<Ref id="ref3">Test message 1</Ref>
<Ref id ="ref4">Test message 2</Ref>
</Body>
</Envelope>
使用 XLST 我想將上面的 XML 轉換如下。我對 XSLT 完全陌生,誰能幫我解決這個問題
預期輸出:
<Envelope>
<Body>
<Ref>
<user>Test1</user>
<company>Comp1</company>
<message>Test message 1</message>
</Ref>
<Ref>
<user>Test2</user>
<company>Comp2</company>
<message>Test message 1</message>
</Ref>
</Body>
</Envelope>
uj5u.com熱心網友回復:
xsl:key我會為Ref元素創建一個,使用@id作為查找鍵。
一個模板,message/@href它使用它的值作為鍵來查找訊息,另一個空模板用于Ref只有訊息的元素,以從輸出中抑制它們。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tei="http://www.tei-c.org/ns/1.0">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:key name="message" match="Ref" use="@id"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="message/@href">
<xsl:value-of select="key('message', substring-after(., '#'))"/>
</xsl:template>
<xsl:template match="Ref[not(*)]"/>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/436290.html
