0

我正在寻找
输入 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>
4

2 回答 2

2

问题是for-each当前上下文元素内部是RoleTypeCT,因此您尝试将模板应用于RoleTypeCT(其中没有)的子元素,而不是其兄弟姐妹。

将其更改为

            <ClientInformation>
                    <xsl:apply-templates select="../*[name()!='RoleTypeCT']"/>
                    <RoleTypeCT><xsl:value-of select="."/></RoleTypeCT>
            </ClientInformation>
于 2013-09-26T15:49:11.533 回答
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>
于 2013-09-26T15:48:47.383 回答