我有一个xpaths的源和目标列表,其中每个列表都基于他们自己的模式。对于每个源路径都存在一个目标路径。例如,我有源列表[/ shiporder/shipfrom/name,/ shiporder/address/city]和目录列表[/ root/Seller/Country],其中列表的顺序连接源与目标路径的路径。现在我可以创建简单易读的样式表,它应用每个源路径并针对目标路径创建输出。对于/运送订单/ shipfrom /名称stylesheet是:通过xpath列表创建xml文件
<xsl:template match="/">
<xsl:apply-templates select="shiporder"/>
</xsl:template>
<xsl:template match="/shiporder">
<root>
<xsl:apply-templates select="shipfrom"/>
</root>
</xsl:template>
<xsl:template match="/shiporder/shipfrom">
<Seller>
<xsl:apply-templates select="name"/>
</Seller>
</xsl:template>
<xsl:template match="/shiporder/shipfrom/name">
<Country>
<xsl:apply-templates select="text()"/>
</Country>
</xsl:template>
和/shiporder/address/city
的stylesheet是:
<xsl:template match="/">
<xsl:apply-templates select="shiporder"/>
</xsl:template>
<xsl:template match="/shiporder">
<root>
<xsl:apply-templates select="address"/>
</root>
</xsl:template>
<xsl:template match="/shiporder/address">
<Seller>
<xsl:apply-templates select="city"/>
</Seller>
</xsl:template>
<xsl:template match="/shiporder/address/city">
<City>
<xsl:apply-templates select="text()"/>
</City>
</xsl:template>
创作后,我上的任意源XML文件同时应用样式基于源模式如:
<shiporder>
<shipfrom>
<name>orderperson1</name>
</shipfrom>
<address>
<city>London</city>
</address>
<address>
<city>Berlin</city>
</address>
</shiporder>
现在我喜欢将两个样式表尽可能简单地组合到一个stylesheet。
对不起,你的问题不是很清楚。请显示两个不同的XML输入和每个输入的期望输出。 –
@JimGarrison我试图简化我的问题,让我知道如果有什么不清楚。如果你对标题有更好的建议,也请告诉我。 – StellaMaris
如果所有元素都存在,您的预期输出是什么?如在你的示例输入XML中一样? –