Xml XSLT:为每个循环复制除一个节点之外的所有节点
我在找Xml XSLT:为每个循环复制除一个节点之外的所有节点,xml,xslt,Xml,Xslt,我在找 输入XML <ClientInformation> <FirstName>Steve</FirstName> <LastName>Jobs</LastName> <MiddleName/>
输入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>
如果我很好地理解您的需求(我不确定您的描述是否等于预期的输出),那么在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>
如果我很好地理解您的需求(我不确定您的描述是否等于预期的输出),那么在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>
问题在于,在每个
的中,当前上下文元素是RoleTypeCT
,因此您试图将模板应用于RoleTypeCT
的子元素(其中没有)而不是其兄弟元素
换成
<ClientInformation>
<xsl:apply-templates select="../*[name()!='RoleTypeCT']"/>
<RoleTypeCT><xsl:value-of select="."/></RoleTypeCT>
</ClientInformation>
问题在于,在每个的中,当前上下文元素是RoleTypeCT
,因此您试图将模板应用于RoleTypeCT
的子元素(其中没有)而不是其兄弟元素
换成
<ClientInformation>
<xsl:apply-templates select="../*[name()!='RoleTypeCT']"/>
<RoleTypeCT><xsl:value-of select="."/></RoleTypeCT>
</ClientInformation>