我正在尝试使用XSLT从给定的输入XML生成XML输出。输出xml将在xml树的各个级别添加不同的命名空间。我使用XPath轴(后代)将命名空间添加到子节点,但它输出子节点旁边的grandchildern节点。
我的输入XML
<?xml version="1.0" encoding="UTF-8"?>
<request>
<header>
<sender>
<User>ServiceUser</User>
<userid type="UserId">ServiceUserid</userid>
</sender>
<receiver>
<Country>IND</Country>
</receiver>
<id>123456</id>
</header>
<Address type="physical">
<subaddress>
<number>2</number>
</subaddress>
<doornumber>13</doornumber>
<streetName>mgr road</streetName>
<locality>cityname</locality>
<postalcode>612453</postalcode>
</Address>
</request>
我的XSLT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aa="xyz.com/aa"
xmlns:bb="xyz.com/bb" xmlns:addr="xyz.com/addr"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
version="1.0">
<xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes" />
<xsl:template match="/">
<soapenv:Envelope>
<soapenv:Header />
<soapenv:Body>
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<!-- template to copy attributes -->
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
<!-- apply the 'event' namespace to the top node and its descendants -->
<xsl:template match="//*">
<xsl:choose>
<!--Add namespace for the root element-->
<xsl:when test="local-name() = 'request'">
<xsl:element name="aa:{local-name()}">
<xsl:namespace name="aa" select="'xyz.com/aa'" />
<xsl:copy-of select="namespace::*" />
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name()}">
<xsl:copy-of select="*" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- template to copy the rest of the nodes -->
<xsl:template match="*[ancestor-or-self::header]">
<xsl:element name="bb:{name()}">
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:template>
<xsl:template match="//Address">
<xsl:element name="aa:{local-name()}">
<xsl:for-each select="//Address/descendant::*">
<xsl:element name="addr:{name()}">
<xsl:apply-templates select="node()|@*" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="comment() | processing-instruction()">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>
必需的输出XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:aa="xyz.com/request" xmlns:bb="xyz.com/header"
xmlns:addr="xyz.com/address">
<soapenv:Header/>
<soapenv:Body>
<aa:request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bb:header>
<bb:sender>
<bb:User>ServiceUser</bb:User>
<bb:userid type="UserId">ServiceUserid</bb:userid>
</bb:sender>
<bb:receiver>
<bb:Country>IND</bb:Country>
</bb:receiver>
<bb:id>123456</bb:id>
</bb:header>
<aa:Address type="physical">
<addr:subaddress>
<number>2</number>
</addr:subaddress>
<addr:doornumber>13</addr:doornumber>
<addr:streetName>mgr road</addr:streetName>
<addr:locality>cityname</addr:locality>
<addr:postalcode>612453</addr:postalcode>
</aa:Address>
</aa:request>
</soapenv:Body>
</soapenv:Envelope>
错误的输出XML:
<soapenv:Envelope xmlns:aa="xyz.com/aa"
xmlns:bb="xyz.com/bb" xmlns:addr="xyz.com/addr"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<aa:request>
<bb:header>
<!-- Correct output here -->
</bb:header>
<aa:Address>
<addr:subaddress>
<number/> <!-- this element is empty -should have value "2" -->
</addr:subaddress>
<addr:number>2</addr:number> <!-- new element added which is incorrect-->
<addr:doornumber>13</addr:doornumber>
<addr:streetName>mgr road</addr:streetName>
<addr:locality>cityname</addr:locality>
<addr:postalcode>612453</addr:postalcode>
</aa:Address>
</aa:request>
</soapenv:Body>
</soapenv:Envelope>
有人可以帮助我使用正确的xslt来产生预期的结果。提前谢谢。
答案 0 :(得分:1)
为什么你不能简单地做到:
XSLT 1.0
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/main', {
templateUrl: 'main.html',
controller: 'MainCtrl'
}).
when('/about', {
templateUrl: 'about.html',
}).
when('/services', {
templateUrl: 'services.html',
}).
when('/contact', {
templateUrl: 'contact.html',
})
}])
注意:SELECT DISTINCT Number = number
FROM master..spt_values
CROSS APPLY STRING_SPLIT(@range,',') AS S
WHERE number BETWEEN PARSENAME(REPLACE(value, '-', '.'), 2)
AND PARSENAME(REPLACE(value, '-', '.'), 1)
声明未被使用,因此完全是多余的。