基本上,我有3个单独的XSLT,它们目前适用于3个不同的XML。相反,我希望有一个XSLT,它可以根据传入的特定内容来决定执行特定的XSLT并得到格式良好的XML。
我试过用以下方法-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:choose>
<xsl:when test="fruits/apples">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<data>
<doc api_type="2" key="20" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:custom="urn:custom.com" ">
<custom:DataSet xyt1:type="tdc:somedataset">
<custom:some_table>
<custom:anothertable>
<xyt1:key>
<xyt1:fruitqty>
<xsl:value-of select="fruit/apple"/>
</xyt1:fruitqty>
</xyt1:key>
<end of this xslt>
</xsl:when>
<xsl:when test="vegetables/tomatoes">
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<data>
<doc api_type="3" key="100" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:custom="urn:custom.com" ">
<custom:DataSet xyt1:type="tdc:somedataset2">
<custom:some_table2>
<custom:anothertable2>
<xyt1:fruitqty>
<xsl:value-of select="fruit/oranges"/>
</xyt1:fruitqty>
<end of this xslt2>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>我会得到两个不同的XML文件
档案1-
<fruits>
<apple>2</apple>
</fruits>档案2-
<vegetables>
<tomatoes>2</tomatoes>
</vegetables>因此,当发现水果/苹果时,我想执行第一个xslt,对蔬菜/西红柿执行另一个xslt。
准确地说,基于输入的XML节点,我希望执行一个特定的XSLT,其中我有单独的工作文件,但我希望它们都在一个XSLT中。
这个方法在正确的块中运行,但是没有输出任何“节点”,它只是输出XML中的值。
例如,它只显示苹果的"2“,而不是像<xyt1:fruitqty> 2 </xyt1:fruitqty>.这样的节点。
因此,我无法获得正确格式化的XML。有什么办法把这件事做好吗?
发布于 2016-08-26 06:15:16
因此,当发现水果/苹果时,我想执行第一个xslt,对蔬菜/西红柿执行另一个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:template match="/">
<output>
<!-- instructions common to all types -->
<xsl:apply-templates/>
</output>
</xsl:template>
<xsl:template match="/fruits">
<!-- instructions for transforming fruits -->
</xsl:template>
<xsl:template match="/vegetables">
<!-- instructions for transforming vegetables -->
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/39157391
复制相似问题