我是XSL的新手。这是我所拥有的XML,通过应用xsl我想在元素/元素节点中插入一个新节点。请参阅下面的输出即时获取和预期输出。我也粘贴了用于获取节点作为输出的xsl。请提供对xsl的更改,以便它可以插入新节点并在层次结构中显示相同的内容。
请提供您宝贵的意见。
<?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="MD0000002524" 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:仅显示带超链接的节点
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<div class="l-d">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="*[*]">
<div class="l-d-t1">
<div class="l-d-t1-t2">
<a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a>
<span class="l-d-t1-t2-s-xpath"><xsl:for-each select="ancestor::*">/<xsl:value-of select="name(.)"/>[<xsl:value-of select="count(preceding-sibling::*[name()=name(current())])+1"/>]</xsl:for-each>/<xsl:value-of select="name(.)"/>[<xsl:value-of select="count(preceding-sibling::*[name()=name(current())])+1"/>]</span>
<span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">
<xsl:value-of select="name()"/>
</span>
</div>
<xsl:apply-templates/>
</div>
</xsl:template>
</xsl:stylesheet>
实际输出:
<?xml version="1.0" encoding="utf-8"?><div class="l-d"><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">plans</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">plan</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/ids[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">ids</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">planInfo</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/productInfo[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">productInfo</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/productInfo[1]/items[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">items</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/productInfo[1]/indicators[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">indicators</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/productInfo[1]/versionInfo[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">versionInfo</span></div></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/forms[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">forms</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/forms[1]/form[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">form</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/forms[1]/form[1]/items[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">items</span></div></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/forms[1]/form[2]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">form</span></div></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">networks</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[1]/network[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[1]/network[2]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[1]/network[3]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[1]/network[4]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[1]/network[5]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[2]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">networks</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[2]/network[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/planInfo[1]/networks[2]/network[2]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">network</span></div></div></div></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/elements[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">elements</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/elements[1]/element[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">element</span></div><div class="l-d-t1"><div class="l-d-t1-t2"><a href="#" onclick="f_l_nav(this);" class="l-d-t1-t2-a">-</a><span class="l-d-t1-t2-s-xpath">/plans[1]/plan[1]/elements[1]/element[1]/indicators[1]</span><span class="l-d-t1-t2-s-name" onclick="f_l_nav(this,1);">indicators</span></div></div></div></div></div></div></div>
预期产出:
- /计划[1]计划 - /计划[1] /计划[1]计划 - /计划[1] /计划[1] / IDS [1] ids- /计划[1] /计划[1] / planInfo [1] planInfo- /计划[1] /计划[1] / planInfo [1] / productInfo [1] productInfo- /计划[1] /计划[1] / planInfo [1] / productInfo [1] /项[1]物品─/计划[1] /计划[1] / planInfo [1] / productInfo [1] /指标[1] indicators- /计划[1] /计划[1] / planInfo [1] / productInfo [1] / VERSIONINFO [1] versionInfo- /计划[1] /计划[1] / planInfo [1] /形式[1] forms- /计划[1] /计划[1] / planInfo [1] /形式[ 1] /形式[1]形状配合/计划[1] /计划[1] / planInfo [1] /形式[1] /形式[1] /项[1]物品─/计划[1] /计划[1 ] / planInfo [1] /形式[1] /形式[2]形式 - /计划[1] /计划[1] / planInfo [1] /网络[1]网络 - /计划[1] /计划[1] / planInfo [1] /网络[1] /网络[1]网络 - /计划[1] /计划[1] / planInfo [1] /网络[1] /网络[2]网络 - /计划[1] /计划[1] / planInfo [1] /网络[1] /网络[3]网络 - /计划[1] /计划[1] / planInfo [1] /网络[1] /网络[4]网络 - /计划[1] /计划[1] / planInfo [1] /网络[1] /网络[5]网络 - /计划[1] /计划[1] / planInfo [1] /网络[2]网络 - /计划[ 1] /计划[1] / planInfo [1] / n的etworks [2] /网络[1]网络 - /计划[1] /计划[1] / planInfo [1] /网络[2] /网络[2]网络 - /计划[1] /计划[1] /元素[1]元素 - /计划[1] /计划[1] /元素[1] /元素[1]元素 - /计划[1] /计划[1] /元素[1] /元素[1] /指标[ 1]指标的 - /计划[1] /计划[1] /元素[1] /元素[1] /参考[1]参考 &LT;
答案 0 :(得分:0)
你可以这样做:复制除elements / element / element之外的每个节点。并创建引用,然后将elements / element /元素放入其中:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pre="http://test.org/schema/product/v1" exclude-result-prefixes="pre">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="pre:elements/pre:element">
<xsl:copy>
<xsl:apply-templates select="node()[not(local-name() = 'element')] | @*"/>
<pre:reference>
<xsl:copy-of select="pre:element"/>
</pre:reference>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
以下解决方案建立在我已经给出here的答案之上。实际上,您可以使用那里的信息来增强当前样式表并解决问题的一半(即插入reference
元素)。
<强>样式表强>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.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:for-each select="v1:element">
<xsl:copy>
<xsl:copy-of select="@*"/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:copy>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
<强>输出强>
<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 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"/>
</reference>
</element>
</elements>
</plan>
</plans>