使用XSLT生成SOAP XML时出现的问题

使用XSLT生成SOAP XML时出现的问题,xml,xslt,soap,Xml,Xslt,Soap,我正在使用XSLT将一些供应XML转换为SOAP请求。但我很难找到一种不需要完整XPATH表达式并且仍然生成有效SOAP XML的方法 下面是配置XML的简化版本 <CreateOrder> <client_info> <name>John Doe</name> <address> <street1>1211 Lakeview Dr.</street1&g

我正在使用XSLT将一些供应XML转换为SOAP请求。但我很难找到一种不需要完整XPATH表达式并且仍然生成有效SOAP XML的方法

下面是配置XML的简化版本

<CreateOrder>
    <client_info>
        <name>John Doe</name>
        <address>
            <street1>1211 Lakeview Dr.</street1>
            <city>New York</city>
            <state>NY</state>
            <country>USA</country>
            <zip>12345</zip>
        </address>
    </client_info>
    <subscriber_number>AAANNNDDDD</subscriber_number>
</CreateOrder>

无名氏
1211湖景博士。
纽约
纽约
美国
12345
安恩德
这里是我正在使用的简化XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" />
    <xsl:template match="/">
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <soapenv:Header/>
            <soapenv:Body>
                <DirectoryNumber><xsl:value-of select="CreateOrder/subscriber_number"/></DirectoryNumber>
                <Locale>
                    <xsl:choose>
                        <xsl:when test="CreateOrder/client_info/address/country = 'USA'">
                            <xsl:text>English (US)</xsl:text>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>User Defined 1</xsl:text>
                        </xsl:otherwise>
                    </xsl:choose>
                </Locale>
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
</xsl:stylesheet>

英语(美国)
用户定义1
这将生成以下XML输出——这正是我所期望/想要的。[请注意,我必须很好地打印此内容-我的输出实际上只是一行,没有换行/缩进。]

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <DirectoryNumber>
            AAANNNDDDD
        </DirectoryNumber>
        <Locale>
            English (US)
        </Locale>
    </soapenv:Body>
</soapenv:Envelope>

安恩德
英语(美国)
通过反复使用,我似乎需要使用
来匹配整个输入文档,否则我就无法将soapxml输入到输出中。是否有其他方法从XSLT生成新XML序列


但是当
存在时,我不能嵌套其他
模板不嵌套。您可以在模板中的适当位置使用
实现所需。在您的简单示例中,不必指定路径,但是在更大、更复杂的样式表中,您可以有许多可重用的模板

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header/>
      <soapenv:Body>
        <DirectoryNumber><xsl:value-of select="CreateOrder/subscriber_number"/></DirectoryNumber>
        <Locale>
          <xsl:apply-templates select="CreateOrder/client_info/address/country"/>
        </Locale>
      </soapenv:Body>
    </soapenv:Envelope>
  </xsl:template>

  <xsl:template match="country">
    <xsl:choose>
      <xsl:when test=". = 'USA'">
        <xsl:text>English (US)</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>User Defined 1</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

英语(美国)
用户定义1

理解XSLT的关键是认识到它不是过程性的。。。您的样式表无法控制。相反,XSLT处理器检查每个输入标记,然后在样式表中搜索匹配的模板。一旦找到并应用了匹配规则,就完成了对该标记的处理。如果匹配
/
,则整个文档将由该模板使用。调用其他模板的唯一方法是通过
,它告诉处理器使用“somexpath”选择的节点重新启动匹配过程。样式表中的第一个模板匹配
/

非常常见,谢谢Jim。作为XSLT的新手,我担心我错过了一些东西,或者有更好的方法来完成我正在做的事情。因此,得到一个仔细解释过的“你就是这么做的”答案真的很有帮助。我将开始使用apply templates构造,使代码更干净/更模块化。