【问题标题】:How to execute different XSLTs within an XSLT?如何在 XSLT 中执行不同的 XSLT?
【发布时间】:2016-08-26 02:18:34
【问题描述】:

基本上,我有 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,然后为蔬菜/西红柿执行另一个。

确切地说,基于输入 XML 节点,我想执行一个特定的 XSLT,我有单独的工作文件,但我希望所有这些都在一个 XSLT 中。

此方法进入正确的块内,但不输出任何“节点”,它只是输出 XML 内的值。

例如 .- 它只为苹果显示“2”,而不是像&lt;xyt1:fruitqty&gt; 2 &lt;/xyt1:fruitqty&gt;.这样的节点

因此,我无法获得格式正确的 XML。任何想法如何做到这一点?

【问题讨论】:

标签: xml xslt xsd xml-parsing


【解决方案1】:

所以我想在找到 fruits/apples 时执行第一个 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>

【讨论】:

  • 感谢您的回答。我已经修改了问题的详细信息。你能复习一下吗?我希望 if else 基于接收到的 XML 并执行适当的 XSLT。
  • 我已编辑我的答案以匹配您的新示例。我确实建议为此使用“if else”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多