簡體   English   中英

XSLT:復制for-each循環中除一個節點之外的所有節點

[英]XSLT: Copy all the nodes except one in for-each loop

我在尋找
輸入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>

所以我想復制除RoleTypeCT以外的所有ClientInformation數據:

<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>

如果我很了解您的需求(我不確定您的描述是否等於預期的輸出)那么您在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內部當前的context元素是RoleTypeCT ,因此您嘗試將模板應用於RoleTypeCT子元素(其中沒有)而不是其兄弟元素。

將其更改為

            <ClientInformation>
                    <xsl:apply-templates select="../*[name()!='RoleTypeCT']"/>
                    <RoleTypeCT><xsl:value-of select="."/></RoleTypeCT>
            </ClientInformation>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM