在新节点下移动节点时发出



我正面临一个重新安排给定输出结构中的节点的问题。Company1,Company2,Company 3是静态的。这些不会改变。所以我需要三个在SCompany列表。

Company4Company5是动态的,因为它们中的一个或两个都可以出现在NCompanyList中。

我需要节点与文本A1,B2,C3SCompany下形成。如果A1在除<Given> <GivenId>以外的任何节点下重复,我不需要这些。

也许我必须使用模式?

输入结构
<root>  
<Given>
<GivenId>testchecki</GivenId>
</Given>
<Given>
<GivenId>STOP</GivenId>
<List>
<ValueGiven>100000</ValueGiven>
</List>
</Given> 
<Given>
<GivenId>A1</GivenId>
<List id="Same">
<ValueGiven>50000</ValueGiven>
</List>
<List id="Different">
<ValueGiven>200000</ValueGiven>
</List>
</Given>
<Given>
<GivenId>B2</GivenId>
<List id="Same">
<ValueGiven>500001</ValueGiven>
</List>
<List id="Different">
<ValueGiven>3000001</ValueGiven>
</List>
</Given>
<Given>
<GivenId>C3</GivenId>
<List id="Same">
<ValueGiven>500002</ValueGiven>
</List>
<List id="Different">
<ValueGiven>3000002</ValueGiven>
</List>
</Given>
<Given>
<GivenId>F4</GivenId>
<Change>EX</Change>
<List id="Same">
<ValueGiven>500003</ValueGiven>
</List>
<List id="Different">
<ValueGiven>3000003</ValueGiven>
</List>
</Given>
<Given>
<GivenId>G5</GivenId>
<Change>Eptest</Change>
<List id="Same">
<ValueGiven>10000004</ValueGiven>
</List>
<List id="Different">
<ValueGiven>2000004</ValueGiven>
</List>
</Given>
</root>

输出结构
<root>
<Scompany>
<A1>
<GivenId></GivenId>
<Same></Same>
<Different></Different>
</A1>
<B2>
<GivenId></GivenId>
<Same></Same>
<Different></Different>
</B2>
<C3>
<GivenId></GivenId>
<Same></Same>
<Different></Different>
</C3>
</Scompany>
<Ncompany>
<F4>
<GivenId></GivenId>
<Same></Same>
<Different></Different>
</F4>
</Ncompany>
<Ncompany>
<G5>
<GivenId></GivenId>
<Same></Same>
<Different></Different>
</G5>
</Ncompany>
</root>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root/Given">
<xsl:if test="position()=6 or position()=7">
<NCompany>
<xsl:apply-templates mode="check"></xsl:apply-templates>
</NCompany>
</xsl:if>
<xsl:if test="position()=3 or position()=4 or position()=5">
<xsl:apply-templates mode="test"></xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template match="root/Given" mode="test">

<xsl:if test="Given/text()='A1'">
<xsl:element name="{concat(name(), position())}">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:element>
</xsl:if>
<xsl:if test="Given/text()='B2'">
<xsl:element name="{concat(name(), position())}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:if>
<xsl:if test="Given/text()='C3'">
<xsl:element name="{concat(name(), position())}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="root/Given" mode="check">
<xsl:if test="position()=6 or position()=7">
<xsl:element name="{concat(name(), position())}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

这是基本思想。您需要调整它以满足您未记录的需求。

<xsl:template match="root">
<xsl:copy>
<xsl:element name="Scompany">
<!-- Select the Given nodes that should be in Scompany. Ensure only one Given per GivenId is selected. -->
<xsl:apply-templates select="Given[GivenId = 'A1'][1]|Given[GivenId = 'B2'][1]|Given[GivenId = 'C3'][1]"/>
</xsl:element>
<xsl:element name="Ncompany">
<!-- Select the given nodes that should be in Ncompany. Ensure only one Given per GivenId is selected.-->
<xsl:apply-templates select="Given[GivenId = 'F4'][1]|Given[GivenId = 'G5'][1]"/>
</xsl:element>
</xsl:copy>
</xsl:template>
<xsl:template match="Given">
<xsl:element name="{GivenId}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="GivenId">
<xsl:copy>
</xsl:copy>
</xsl:template>
<xsl:template match="List">
<xsl:choose>
<xsl:when test="@id='Same'">
<xsl:element name="Same">
</xsl:element>
</xsl:when>    
<xsl:when test="@id='Different'">
<xsl:element name="Different">
</xsl:element>
</xsl:when>    
</xsl:choose>
</xsl:template>
<xsl:template match="Change"/>
<!-- This is the bottom -->

最新更新