[英]How to add prefixed type into repeating parent node and select all elements for each using XSLT?
我想知道如何向每个重复的父节点添加前缀类型[xsi:type“ ...”],并选择其所有子节点。 xml是soap信封,输出是信封的标头和主体的修改。 样本输入xml是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://Example1.com"
xmlns:ns1="http://Sample1.com">
<soapenv:Header>
<ns:SessionHeader>
<ns:Auth>a1b2c4d5</ns:Auth>
</ns:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<ns:Create>
<!--Zero or more repetitions:-->
<ns:BankAccount>
<ns1:AccountNumber>1111</ns1:AccountNumber>
<ns1:BillingState>Texas</ns1:BillingState>
<ns1:Name>John</ns1:Name>
<ns1:Phone>+1 111</ns1:Phone>
<ns1:Site>Chicago</ns1:Site>
<ns1:Website>www.site1.com</ns1:Website>
.
.
</ns:BankAccount>
<ns:BankAccount>
<ns1:AccountNumber>222</ns1:AccountNumber>
<ns1:BillingState>Hawai</ns1:BillingState>
<ns1:Name>Bob</ns1:Name>
<ns1:Phone>+2 222</ns1:Phone>
<ns1:Site>Canada</ns1:Site>
<ns1:Website>www.site2.com</ns1:Website>
.
.
</ns:BankAccount>
.
.
.
</ns:Create>
</soapenv:Body>
</soapenv:Envelope>
给定输入xml所需的输出xml是
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://Example1.com"
xmlns:ns1="http://Sample1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns:SessionHeader>
<ns:Auth>a1b2c4d5</ns:Auth>
</ns:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<ns:Create>
<ns:BankAccount xsi:type="Account" xmlns="http://Example1.com" >
<ns1:AccountNumber>1111</ns1:AccountNumber>
<ns1:BillingState>Texas</ns1:BillingState>
<ns1:Name>John</ns1:Name>
<ns1:Phone>+1 111</ns1:Phone>
<ns1:Site>Chicago</ns1:Site>
<ns1:Website>www.site1.com</ns1:Website>
.
.
</ns:BankAccount>
<ns:BankAccount xsi:type="Account" xmlns="http://Example1.com" >
<ns1:AccountNumber>222</ns1:AccountNumber>
<ns1:BillingState>Hawai</ns1:BillingState>
<ns1:Name>Bob</ns1:Name>
<ns1:Phone>+2 222</ns1:Phone>
<ns1:Site>Canada</ns1:Site>
<ns1:Website>www.site2.com</ns1:Website>
.
.
</ns:BankAccount>
.
.
.
</ns:Create>
</soapenv:Body>
</soapenv:Envelope>
我已尝试使用以下提到的XSL,但未产生所需的结果。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://Example1.com"
xmlns:ns1="http://Sample1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns:SessionHeader>
<ns:Auth>
<xsl:value-of select="//ns:Auth"/>
</ns:Auth>
</ns:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<ns:Create>
<ns:BankAccount xsi:type="Account" xmlns="http://Example1.com">
<xsl:apply-template select="node()"/>
</ns:BankAccount>
</ns:Create>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
我不确定我哪里出错了。 我是否应该首先获取所有父级“ BankAccount”节点及其元素以添加前缀,然后再构建肥皂信封? 请指导我。
如果仅要将xsi:type="Account"
到每个ns:BankAccount
元素,请使用带有ns:BankAccount
专用模板的身份转换 ,该模板在添加属性的同时复制内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://Example1.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns:BankAccount">
<xsl:copy>
<xsl:attribute name="xsi:type">Account</xsl:attribute>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.