输入XML:
<PostedOrder>
<ID>12345</ID>
<Email>abc@gmail.com</Email>
<Sender>John</Sender>
<Recepient>Mary</Recepient>
<ShipTo>Delhi</ShipTo>
<ShipFrom>Mumbai</ShipFrom>
<ShipUnit>MAIL</ShipUnit>
</PostedOrder>
输出XML:
<PostedOrder>
<OrderID>12345</OrderID>
<Contact>abc@gmail.com</Contact>
<SentTo>Mary</SentTo>
<Sentfrom>John</Sentfrom>
<To>Delhi</To>
<From>Mumbai</From>
<ShipUnitID>
<LocationID>
<Bid>
<Domain>Buisness</Domain><!-- Hardcoded value-->
<Xid>MAIL</Xid>
</Bid>
</LocationID>
</ShipUnitID>
</PostedOrder>
XSL:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space elements="" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<PostedOrder>
<xsl:for-each select="/PostedOrder">
<OrderID><xsl:value-of select="ID" /></OrderID>
<Contact><xsl:value-of select="Email" /></Contact>
<SentTo><xsl:value-of select="Recepient" /></SentTo>
<Sentfrom><xsl:value-of select="Sender" /></Sentfrom>
<To><xsl:value-of select="ShipTo" /></To>
<From><xsl:value-of select="ShipFrom" /></From>
<ShipUnitID>
<LocationID>
<Bid>
<Domain>Buisness</Domain><!-- Hardcoded value-->
<Xid>
<xsl:value-of select="ShipUnit" />
</Xid>
</Bid>
</LocationID>
</ShipUnitID>
</xsl:for-each>
</PostedOrder>
</xsl:template>
</xsl:stylesheet>
在上面的示例中,如果使用XSLT在输入XML中缺少<ShipUnitID>
,则我要求不在输出XML中创建<ShipUnit>
和后续子元素。有人可以帮助我,我是XML转换的新手。提前致谢。
答案 0 :(得分:0)
由于您不需要输出中的<ShipUnitID>
元素及其子元素,因此您可以在准备<xsl:if>
之前添加<ShipUnit>
条件以检查<ShipUnitID>
元素。以下是供参考的代码。
<xsl:if test="ShipUnit != ''" >
<ShipUnitID>
<LocationID>
<Bid>
<Domain>Buisness</Domain>
<Xid><xsl:value-of select="ShipUnit" /></Xid>
</Bid>
</LocationID>
</ShipUnitID>
</xsl:if>
为了测试这一点,我们可以修改输入XML,使其具有<PostedOrder>
的2个元素,一个<ShipUnit>
作为子元素,另一个元素不作为子元素。
<PostedOrders>
<PostedOrder>
<ID>12345</ID>
<Email>abc@gmail.com</Email>
<Sender>John</Sender>
<Recepient>Mary</Recepient>
<ShipTo>Delhi</ShipTo>
<ShipFrom>Mumbai</ShipFrom>
<ShipUnit>MAIL</ShipUnit>
</PostedOrder>
<PostedOrder>
<ID>23456</ID>
<Email>xyz@gmail.com</Email>
<Sender>Paul</Sender>
<Recepient>Tom</Recepient>
<ShipTo>Chennai</ShipTo>
<ShipFrom>Bengaluru</ShipFrom>
</PostedOrder>
</PostedOrders>
更新了XSLT :已修改XSLT以匹配模板<PostedOrder>
,而不是匹配根元素"/"
,从而无需<xsl:for-each>
。< / p>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="PostedOrder">
<PostedOrder>
<OrderID><xsl:value-of select="ID" /></OrderID>
<Contact><xsl:value-of select="Email" /></Contact>
<SentTo><xsl:value-of select="Recepient" /></SentTo>
<Sentfrom><xsl:value-of select="Sender" /></Sentfrom>
<To><xsl:value-of select="ShipTo" /></To>
<From><xsl:value-of select="ShipFrom" /></From>
<xsl:if test="ShipUnit != ''" >
<ShipUnitID>
<LocationID>
<Bid>
<Domain>Buisness</Domain>
<Xid><xsl:value-of select="ShipUnit" /></Xid>
</Bid>
</LocationID>
</ShipUnitID>
</xsl:if>
</PostedOrder>
</xsl:template>
</xsl:stylesheet>
输出
<PostedOrder>
<OrderID>12345</OrderID>
<Contact>abc@gmail.com</Contact>
<SentTo>Mary</SentTo>
<Sentfrom>John</Sentfrom>
<To>Delhi</To>
<From>Mumbai</From>
<ShipUnitID>
<LocationID>
<Bid>
<Domain>Buisness</Domain>
<Xid>MAIL</Xid>
</Bid>
</LocationID>
</ShipUnitID>
</PostedOrder>
<PostedOrder>
<OrderID>23456</OrderID>
<Contact>xyz@gmail.com</Contact>
<SentTo>Tom</SentTo>
<Sentfrom>Paul</Sentfrom>
<To>Chennai</To>
<From>Bengaluru</From>
</PostedOrder>