Insert New node

2019-08-21 20:25发布

Here is the XML i have, by applying xsl i want to insert a new node inside the elements/element node. See below the output im getting now and the expected output. also i m pasting the xsl i used to get the nodes as output. Please provide a chaneg to the xsl so tht it can insert a new node and display the same in the hierachy.

Please provide your valuable inputs.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<plans xmlns="http://test.org/schema/product/v1">
<plan effDate="2013-07-01" endDate="9999-12-31" id="MD4" source="PDM" state="RELEASED" version="I.8" vertical="MEDICAL">
    <!--Generated by: GeneratePDMCanonical ver. 16.4-->
    <!--Codeset version 1.6-->
    <!--Generated by: FilterPDM ver. 8-->
    <ids WINDCHILL="MD0000002524">
        <id type="WINDCHILL">1</id>
        <id type="BOC">1</id>
    </ids>
    <planInfo>
        <productInfo source="PDM">
            <claimAdjudicationBeginDate>2013-07-01</claimAdjudicationBeginDate>
            <items>
                <!--Unmapped items-->
                <item name="baselineId">000000</item>
            </items>
            <indicators>
                <indicator type="HSAQualified">false</indicator>
                <!--Unmapped indicators-->
                <indicator type="hsaQualified">false</indicator>
            </indicators>
            <versionInfo>
                <version>I</version>
            </versionInfo>
        </productInfo>
        <forms>
            <form id="FR0000001378" type="FilingForm" version="A.1">
                <formType>MA PPO</formType>
                <legalEntity>Company</legalEntity>
                <ratingState>MA</ratingState>
                <items>
                    <!--Unmapped items-->
                    <item name="contentBaselineNumber"/>
                </items>
            </form>
            <form id="1379" type="HandbookForm" version="A.1">
                <formType>Fully Insured</formType>
                <formNumber/>
            </form>
        </forms>
        <!--Plan Level Networks-->
        <networks id="Plan">
            <network id="NT0300000029" name="Tier 3" version="A.1">
                <networkCode>029</networkCode>
                <networkName>Tier 3</networkName>
                <networkType>IN</networkType>
                <networkLevel>3</networkLevel>
            </network>
        </networks>
    </planInfo>
    <elements>
        <!--Plan-Level Deductibles-->
        <element id="BN000001" parent="BN09" type="" version="A.3">
            <forService>Breast </forService>
            <elementValue forService="Breast "/>
            <!--Defined for plans with fundingArrangement: Fully Insured-->
            <indicators>
                <indicator type="referralRequired">false</indicator>
                <indicator type="payAlways">false</indicator>
            </indicators>
            <networks ref="Plan"/>
            <defaultCMSLocation>12</defaultCMSLocation>
            <!--costshare applies:applyCoinsurance|applyDeductible|applyOONCoins|applyOONDeductible|applyToOOPMax-->
            <element name="20% 1" networkType="IN" ref="C005" type="CoInsurance" version="A.7"/>
            <element name="$1000 IN -  Tier 1" networkType="IN" ref="DD0" type="Deductible" version="B.2"/>
       </element>
    </elements>
</plan>

XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="*[*]">
        <xsl:copy>
            <xsl:apply-templates select="*[*]" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|elements/*">

        <xsl:copy>
            <reference>
                <xsl:apply-templates select="@*|elements/child::*"/>
                </reference>
        </xsl:copy>

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

Output:

<plans xmlns="http://hphc.org/schema/product/v1">
   <plan>
      <ids/>
      <planInfo>
         <productInfo>
            <items/>
            <indicators/>
            <versionInfo/>
         </productInfo>
         <forms>
            <form>
               <items/>
            </form>
            <form/>
         </forms>
         <networks>
            <network/>
         </networks>
      </planInfo>
      <elements>
         <element>
            <indicators/>
         </element>
      </elements>
   </plan>
</plans>

Expected Output:

<elements>
             <element>
                <indicators/>
 -- new node--  <reference/>    -- new node--
             </element>
      </elements>

标签: xml xslt
1条回答
劫难
2楼-- · 2019-08-21 20:27

As long as your template match refers to wildcards like *:

<xsl:template match="*[*]">

you do not have to worry about namespaces. However, if you match specific elements from your input XML like here:

<xsl:template match="@*|elements/*">

you have to adhere to the default namespace specified. The following line defines a default namespace:

<plans xmlns="http://test.org/schema/product/v1">

which is passed on to all child elements as well. In other words, the element element has a namespace, too. As far as the XSLT processor is concerned, v1:element and element are entirely different elements.

By the way,

  • you do not need to select all attributes (@*) because they are not matched anyway.
  • in your context (template match for children of elements elements) an expression like elements/child::*" selects all children of the children of a child of an elements element that are called "elements". Which is confusing and not what you want, I think.
  • as can be seen from the second remark, please avoid giving elements names like "elements" and "element" whenever possible

Stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:v1="http://test.org/schema/product/v1"
   exclude-result-prefixes="v1">

    <xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="*[*]">
        <xsl:copy>
            <xsl:apply-templates select="*[*]" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="v1:element[*]">
        <xsl:copy>
           <xsl:apply-templates select="*"/>
           <xsl:element name="reference" namespace="http://test.org/schema/product/v1"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>

Output

<plans xmlns="http://test.org/schema/product/v1">
   <plan>
      <ids/>
      <planInfo>
         <productInfo>
            <items/>
            <indicators/>
            <versionInfo/>
         </productInfo>
         <forms>
            <form>
               <items/>
            </form>
            <form/>
         </forms>
         <networks>
            <network/>
         </networks>
      </planInfo>
      <elements>
         <element>
            <indicators/>
            <reference/>
         </element>
      </elements>
   </plan>
</plans>
查看更多
登录 后发表回答