我正在寻找
输入XML
<ClientInformation>
<FirstName>Steve</FirstName>
<LastName>Jobs</LastName>
<MiddleName/>
<DateOfBirth>09/18/2013</DateOfBirth>
<TaxIdentification>213465</TaxIdentification>
<ClientDetailPK>52385</ClientDetailPK>
<RoleTypeCT>OWN</RoleTypeCT>
<RoleTypeCT>IBE</RoleTypeCT>
<RoleTypeCT>Insured</RoleTypeCT>
</ClientInformation>
到输出Xml
<SaveData>
<ClientInformation>
<FirstName>Steve</FirstName>
<LastName>Jobs</LastName>
<MiddleName/>
<DateOfBirth>09/18/2013</DateOfBirth>
<TaxIdentification>213465</TaxIdentification>
<ClientDetailPK>52385</ClientDetailPK>
<RoleTypeCT>OWN</RoleTypeCT>
</ClientInformation>
<ClientInformation>
<FirstName>Steve</FirstName>
<LastName>Jobs</LastName>
<MiddleName/>
<DateOfBirth>09/18/2013</DateOfBirth>
<TaxIdentification>213465</TaxIdentification>
<ClientDetailPK>52385</ClientDetailPK>
<RoleTypeCT>IBE</RoleTypeCT>
</ClientInformation>
<ClientInformation>
<FirstName>Steve</FirstName>
<LastName>Jobs</LastName>
<MiddleName/>
<DateOfBirth>09/18/2013</DateOfBirth>
<TaxIdentification>213465</TaxIdentification>
<ClientDetailPK>52385</ClientDetailPK>
<RoleTypeCT>Insured</RoleTypeCT>
</ClientInformation>
<SaveData>
所以我想复制除ClientInformation
以外的所有RoleTypeCT
数据:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="ClientInformation">
<SaveData>
<xsl:for-each select="RoleTypeCT">
<ClientInformation>
<xsl:apply-templates select="*[name()!='RoleTypeCT']"/>
<RoleTypeCT><xsl:value-of select="."/></RoleTypeCT>
</ClientInformation>
</xsl:for-each>
</SaveData>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
如果我很了解您的需求(我不确定您的描述是否等于预期的输出)那么您在for-each期间遇到上下文更改的问题。请根据样式表尝试使用xslt。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="ClientInformation">
<!-- Store actual element to preserve it from context changes during for-each-->
<xsl:variable name="currentClientInformation" select="." />
<SaveData>
<xsl:for-each select="RoleTypeCT">
<!-- Store actual RoleTypeCT -->
<xsl:variable name="currRoleTypeCT" select="." />
<ClientInformation>
<xsl:apply-templates select="$currentClientInformation/*[not(self::RoleTypeCT)]"/>
<RoleTypeCT>
<xsl:value-of select="$currRoleTypeCT" />
</RoleTypeCT>
</ClientInformation>
</xsl:for-each>
</SaveData>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
问题是for-each
当前上下文元素是RoleTypeCT
,所以你试图将模板应用于RoleTypeCT
的子元素(其中没有而不是它的兄弟姐妹。
将其更改为
<ClientInformation>
<xsl:apply-templates select="../*[name()!='RoleTypeCT']"/>
<RoleTypeCT><xsl:value-of select="."/></RoleTypeCT>
</ClientInformation>