XSLT:将“上级父级”属性复制到子级元素

时间:2019-03-01 11:00:41

标签: xml xslt

我是XSLT的新手,所以这可能是基础知识,但是我非常感谢您的投入。我需要复制位于节点的“父”父级的元素的属性值。

以下是我的xml

<session>
          <data>
             <policy>
               <DomicileState>NJ</DomicileState>
               <PrimaryRatingState>NJ</PrimaryRatingState>
                <AvailableNumberIDForCurrentTerm>1</AvailableNumberIDForCurrentTerm>
               <EffectiveDate>2019-02-20</EffectiveDate>
               <Term>12</Term>
               <ExpirationDate>2020-02-20</ExpirationDate>
                <line>
                <Type>BusinessOwners</Type>
                <lineUnderwriting>
                    <WritingCompany>07</WritingCompany>
                </lineUnderwriting>
                <limit>
                     <Type>LiabilityPerOccurenceLimit</Type>
                     <iValue>500000</iValue>
                  </limit>
                <limit>
                     <Type>ProductsCompletedAggregateLimit</Type>
                     <iValue>1000000</iValue>
                  </limit>
                <limit>
                     <Type>GeneralAggregateLimit</Type>
                     <iValue>1000000</iValue>
                  </limit>
                <limit>
                     <Type>MedicalExpensesLimit</Type>
                     <iValue>0</iValue>
                  </limit>
                <limit>
                     <Type>PropertyDamageDeductible</Type>
                     <iValue>0</iValue>
                  </limit>
                 <risk>
                 <locationid></locationid>
                 <buildingid></buildingid>
                <occupancyid></occupancyid>
                  <!-- <coverage> -->
                     <!-- <Type>RiskLiability</Type> -->
                     <!-- <ClassCodeDisplay>7|[65145] Apartment Building - 4 families or fewer, with office occupancy - includes 3- or 4- fami...</ClassCodeDisplay> -->
                  <!-- </coverage> -->
                  <!-- <coverage> -->
                     <!-- <Type>Building</Type> -->
                     <!-- <limit> -->
                <!-- <Type>Standard</Type> -->
                <!-- <iValue>299999</iValue> -->
               <!-- </limit> -->
                  <!-- </coverage> -->
                  </risk>
                </line>
               <ProductNumber />
               <ProfitCenter />
               <QuoteNumber />
               <PolicyNumber />
               <ProductName>194</ProductName>
               <ProductNameDisplay>BOP</ProductNameDisplay>
               <Status />
               <Department>96</Department>
               <DepartmentDisplay>Service Stations</DepartmentDisplay>
               <TransactionDate>2019-02-01</TransactionDate>
              <PolicyType>New</PolicyType>
              <policyProcessing>
                  <CurrentPolicyNumberIdentifier></CurrentPolicyNumberIdentifier>
                </policyProcessing>
             </policy>
             <account>
                <Name>Test_XMLRequest_1.2</Name>
                <address>
                 <Address1>23rd Main Suite</Address1>
                 <Address2>Agoura Hills</Address2>
                 <City>Newark</City>
                 <State>NJ</State>
                 <ZipCode>07001</ZipCode>
                </address>
                <LoadFromSubmissionIndicator>0</LoadFromSubmissionIndicator>
                <PrimaryPhone>1234567890</PrimaryPhone>
                <Email>smtpar@gmail.com</Email>
                <InsuredDBA />
                <Country>UnitedStates</Country>
                <POBox />
                <location id="locationid">
                  <address>
                     <State>NJ</State>
                     <Address2>Agoura Hills</Address2>
                     <City>Newark</City>
                     <ZipCode>07001</ZipCode>
                     <Address1>23rd Main Suite</Address1>
                  </address>
                  <Number>1</Number>
                  <locationBusinessOwners>
                  <County>Atlantic</County>
                  <Territory>001</Territory>
                  <ProtectionClass>01</ProtectionClass>
                  <GradeType>IndividuallyInspected</GradeType>
                  <BuildingCodeEffectivenessGrading>1</BuildingCodeEffectivenessGrading>
                  </locationBusinessOwners>
                </location>
              <building id="buildingid">
                 <YearBuilt>2016</YearBuilt>
                 <BuildingLocationNumber>1</BuildingLocationNumber>
                 <LocationBuildingNumber>1</LocationBuildingNumber>
              </building >
              <occupancy id="occupancyid">
                  <SearchValueBOP>Building</SearchValueBOP>
                  <SearchResultsBOP>7|[65145] Apartment Building - 4 families or fewer, with office occupancy - includes 3- or 4- fami...</SearchResultsBOP>
                  <BOP_SquareFootage>2000</BOP_SquareFootage>
                  <Eligible>1</Eligible>
              </occupancy>
            </account>
             <policyAdmin>
                <transactions>
                  <transaction>
                  <Type>New</Type>  
                  <TermPremium></TermPremium>
                  <PriorPremium></PriorPremium>
                  <NewPremium></NewPremium>
                  </transaction>
                </transactions>
             </policyAdmin>
             <userDetails>
                <userName>abc@domain.com</userName>
                <fullName>Express Admin</fullName>
             </userDetails>
             <producerinfo>
                <Code>83332</Code>
                <Name>Test Producer Alive</Name>
             </producerinfo>
             <agencyDetails>
                <agencyID>10003</agencyID> 
                <name>Businessowners</name>
                <entityTypeID>2</entityTypeID>
            <entityType>Carrier</entityType>
             </agencyDetails>
          </data>
    </session>

我需要复制以下值

  • 风险/ buildingid元素中建筑物属性的值。
  • 风险/ locationid元素内的位置属性值。
  • risk / occupancyid元素内的“占用率”属性值。

以下是我的xslt。

 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>

  <xsl:template match="@* | node()">
  <xsl:copy>
  <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
  </xsl:template>
  <xsl:template match="risk">

    <locationid><xsl:value-of select="../../../account/location/@id"/> 
    </locationid> 
    <occupancyid><xsl:value-of select="../../../account/occupancy/@id"/> 
    </occupancyid>

   </xsl:template>
  </xsl:stylesheet>

当前输出:

<data>
             <policy>
                 <DomicileState>NJ</DomicileState>
                 <PrimaryRatingState>NJ</PrimaryRatingState>
                    <AvailableNumberIDForCurrentTerm>1</AvailableNumberIDForCurrentTerm>
                 <EffectiveDate>2019-02-20</EffectiveDate>
                 <Term>12</Term>
                 <ExpirationDate>2020-02-20</ExpirationDate>
                    <line>
                       <Type>BusinessOwners</Type>
                       <lineUnderwriting>
                             <WritingCompany>07</WritingCompany>
                       </lineUnderwriting>
                       <limit>
                             <Type>LiabilityPerOccurenceLimit</Type>
                             <iValue>500000</iValue>
                       </limit>
                       <limit>
                             <Type>ProductsCompletedAggregateLimit</Type>
                             <iValue>1000000</iValue>
                       </limit>
                       <limit>
                             <Type>GeneralAggregateLimit</Type>
                             <iValue>1000000</iValue>
                       </limit>
                       <limit>
                             <Type>MedicalExpensesLimit</Type>
                             <iValue>0</iValue>
                       </limit>
                       <limit>
                             <Type>PropertyDamageDeductible</Type>
                             <iValue>0</iValue>
                       </limit>
                       <locationid>locationid</locationid>
        <occupancyid>occupancyid</occupancyid>
                    </line>

预期输出:

<data>
             <policy>
                 <DomicileState>NJ</DomicileState>
                 <PrimaryRatingState>NJ</PrimaryRatingState>
                    <AvailableNumberIDForCurrentTerm>1</AvailableNumberIDForCurrentTerm>
                 <EffectiveDate>2019-02-20</EffectiveDate>
                 <Term>12</Term>
                 <ExpirationDate>2020-02-20</ExpirationDate>
                    <line>
                       <Type>BusinessOwners</Type>
                       <lineUnderwriting>
                             <WritingCompany>07</WritingCompany>
                       </lineUnderwriting>
                       <limit>
                             <Type>LiabilityPerOccurenceLimit</Type>
                             <iValue>500000</iValue>
                       </limit>
                       <limit>
                             <Type>ProductsCompletedAggregateLimit</Type>
                             <iValue>1000000</iValue>
                       </limit>
                       <limit>
                             <Type>GeneralAggregateLimit</Type>
                             <iValue>1000000</iValue>
                       </limit>
                       <limit>
                             <Type>MedicalExpensesLimit</Type>
                             <iValue>0</iValue>
                       </limit>
                       <limit>
                             <Type>PropertyDamageDeductible</Type>
                             <iValue>0</iValue>
                       </limit>
                       <risk>
                          <locationid>locationid</locationid>
           <occupancyid>occupancyid</occupancyid>

                       </risk>
                    </line>

我能够从属性中获取数据,但无法将其复制到所需位置。任何帮助或建议都会很有帮助。

1 个答案:

答案 0 :(得分:0)

尝试一下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="risk">
        <xsl:element name="risk">
            <locationid>
                <xsl:value-of select="../../../account/location/@id"/>
            </locationid>
            <occupancyid>
                <xsl:value-of select="../../../account/occupancy/@id"/>
            </occupancyid>
            <buildingid>
                <xsl:value-of select="../../../account/building/@id"/>
            </buildingid>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>