C# XSLT,将模板结果转换为变量并动态构造新的XML
我有一个xml,希望通过XSLT处理xml。 在这里,我可以通过应用模板生成xml,现在我想使用这个模板输出作为同一XSL中另一个输入的输入(动态) i、 e.我想将当前输出作为输入传递给模板output2 Output1:第一个模板,结果R1C# XSLT,将模板结果转换为变量并动态构造新的XML,c#,xml,xslt,C#,Xml,Xslt,我有一个xml,希望通过XSLT处理xml。 在这里,我可以通过应用模板生成xml,现在我想使用这个模板输出作为同一XSL中另一个输入的输入(动态) i、 e.我想将当前输出作为输入传递给模板output2 Output1:第一个模板,结果R1 Output2:第二个模板,需要生成最终输出 输入XML <rules> <emie> <domain exclude="true">sww-epw3.testing.com</do
Output2:第二个模板,需要生成最终输出 输入XML
<rules>
<emie>
<domain exclude="true">sww-epw3.testing.com</domain>
<domain>sww-epw3.testing.com
<path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang">/Apps/dsprm</path>
<path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang">/Apps/uaprm</path>
<path docMode="7" comment="APEXID:102994;AppName:Physical Records Management;StepOut:22 May 2015;StepIn:22 May 2016;Requester:Per Roseth;BusinessUnit:Upstream;PortfolioManager:Junfeng Liang">/Apps/ptprm</path>
<path docMode="7" comment="APEXID:141923;MyRequestID:7512579;AppName:Production Revenue Accounting-PRA;StepOut:4 March 2016;StepIn:28 February 2017;Requester:PAUL.GEBBIE@testing.COM;BusinessUnit:UI;PortfolioManager:Per.Roseth@testing.com;Remarks:On Board Application">/apps/doto</path>
<path docMode="7" comment="APEXID:;MyRequestID:7219068;AppName:;StepOut:9 December 2015;StepIn:;Requester:SAIKAT.MANNA@testing.COM;BusinessUnit:UA;PortfolioManager:;Remarks:Technical Assessment">/apps/seprm</path>
<path docMode="7" comment="APEXID:;MyRequestID:7219068;AppName:;StepOut:9 Dec 2015;StepIn:;Requester:SAIKAT.MANNA@testing.COM;BusinessUnit:UA;PortfolioManager:;Remarks:Technical Assessment">/apps/gfprm</path>
</domain>
</emie>
</rules>
sww-epw3.testing.com
sww-epw3.testing.com
/应用程序/dsprm
/应用程序/uaprm
/应用程序/ptprm
/应用程序/doto
/apps/seprm
/应用程序/gfprm
**XSLT**
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:copy-of select="$R2"/>
</xsl:template>
<xsl:variable name="R1">
<xsl:call-template name="output1" />
</xsl:variable>
<xsl:variable name="R2">
<xsl:call-template name="output2" />
</xsl:variable>
<xsl:template name="output2">
<xsl:element name="DOMAIN">
<xsl:element name="APEXID"></xsl:element>
<xsl:element name="MYREQUESTID"></xsl:element>
<xsl:element name="APPLICATION_NAME"></xsl:element>
<xsl:element name="URL"></xsl:element>
<xsl:element name="MODE"></xsl:element>
<xsl:element name="STEPOUT"></xsl:element>
<xsl:element name="STEPIN"></xsl:element>
<xsl:element name="BU"></xsl:element>
<xsl:element name="PORTFOLIO_MGR"></xsl:element>
<xsl:element name="REMARKS"></xsl:element>
<xsl:element name="RAG_STATUS"></xsl:element>
<xsl:element name="APEX_DATA_PORTAL_URL"></xsl:element>
</xsl:element>
</xsl:template>
<xsl:template name="output1">
<xsl:variable name="site">
<xsl:value-of select="rules/emie/domain/text()"/>
</xsl:variable>
<emie>
<xsl:for-each select="rules/emie/domain/path">
<domain>
<xsl:element name="URL">
<xsl:value-of select="concat($site,text())" />
</xsl:element>
<xsl:apply-templates select="@comment"/>
</domain>
</xsl:for-each>
<xsl:for-each select="domain">
<domain>
<xsl:element name="URL">
<xsl:value-of select="concat($site,text())" />
</xsl:element>
<xsl:apply-templates select="@comment"/>
</domain>
</xsl:for-each>
</emie>
</xsl:template>
<xsl:template match="@comment" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="';'"/>
<xsl:param name="separator2" select="':'"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:choose>
<xsl:when test="(contains($text, $separator2))">
<xsl:variable name="token" select="normalize-space(substring-before($text, $separator2))" />
<xsl:element name="{$token}">
<xsl:value-of select="normalize-space(substring-after($text, $separator2))"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="AppName">
<xsl:value-of select="$text"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-before($text, $separator)"/>
</xsl:call-template>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
电流输出
<emie>
<domain>
<URL>sww-epw3.testing.com/Apps/dsprm</URL>
<APEXID>102994</APEXID>
<AppName>Physical Records Management</AppName>
<StepOut>22 May 2015</StepOut>
<StepIn>22 May 2016</StepIn>
<Requester>Per Roseth</Requester>
<BusinessUnit>Upstream</BusinessUnit>
<PortfolioManager>Junfeng Liang</PortfolioManager>
</domain>
<domain>
<URL>sww-epw3.testing.com/Apps/uaprm</URL>
<APEXID>102994</APEXID>
<AppName>Physical Records Management</AppName>
<StepOut>22 May 2015</StepOut>
<StepIn>22 May 2016</StepIn>
<Requester>Per Roseth</Requester>
<BusinessUnit>Upstream</BusinessUnit>
<PortfolioManager>Junfeng Liang</PortfolioManager>
</domain>
<domain>
<URL>sww-epw3.testing.com/Apps/ptprm</URL>
<APEXID>102994</APEXID>
<AppName>Physical Records Management</AppName>
<StepOut>22 May 2015</StepOut>
<StepIn>22 May 2016</StepIn>
<Requester>Per Roseth</Requester>
<BusinessUnit>Upstream</BusinessUnit>
<PortfolioManager>Junfeng Liang</PortfolioManager>
</domain>
<domain>
<URL>sww-epw3.testing.com/apps/doto</URL>
<APEXID>141923</APEXID>
<MyRequestID>7512579</MyRequestID>
<AppName>Production Revenue Accounting-PRA</AppName>
<StepOut>4 March 2016</StepOut>
<StepIn>28 February 2017</StepIn>
<Requester>PAUL.GEBBIE@testing.COM</Requester>
<BusinessUnit>UI</BusinessUnit>
<PortfolioManager>Per.Roseth@testing.com</PortfolioManager>
<Remarks>On Board Application</Remarks>
</domain>
<domain>
<URL>sww-epw3.testing.com/apps/seprm</URL>
<APEXID></APEXID>
<MyRequestID>7219068</MyRequestID>
<AppName></AppName>
<StepOut>9 December 2015</StepOut>
<StepIn></StepIn>
<Requester>SAIKAT.MANNA@testing.COM</Requester>
<BusinessUnit>UA</BusinessUnit>
<PortfolioManager></PortfolioManager>
<Remarks>Technical Assessment</Remarks>
</domain>
<domain>
<URL>sww-epw3.testing.com/apps/gfprm</URL>
<APEXID></APEXID>
<MyRequestID>7219068</MyRequestID>
<AppName></AppName>
<StepOut>9 Dec 2015</StepOut>
<StepIn></StepIn>
<Requester>SAIKAT.MANNA@testing.COM</Requester>
<BusinessUnit>UA</BusinessUnit>
<PortfolioManager></PortfolioManager>
<Remarks>Technical Assessment</Remarks>
</domain>
</emie>
sww-epw3.testing.com/Apps/dsprm
102994
实物记录管理
2015年5月22日
2016年5月22日
每玫瑰
上游
梁俊峰
sww-epw3.testing.com/Apps/uaprm
102994
实物记录管理
2015年5月22日
2016年5月22日
每玫瑰
上游
梁俊峰
sww-epw3.testing.com/Apps/ptprm
102994
实物记录管理
2015年5月22日
2016年5月22日
每玫瑰
上游
梁俊峰
sww-epw3.testing.com/apps/doto
141923
7512579
生产收入会计
2016年3月4日
2017年2月28日
保罗。GEBBIE@testing.COM
用户界面
每。Roseth@testing.com
随车申请
sww-epw3.testing.com/apps/seprm
7219068
2015年12月9日
赛卡特。MANNA@testing.COM
UA
技术评估
sww-epw3.testing.com/apps/gfprm
7219068
2015年12月9日
赛卡特。MANNA@testing.COM
UA
技术评估
预期输出-需要包含更多新元素并处理输入xml(output1)的数据,例如日期格式化
<emie><domain>
<URL>sww-epw3.testing.com/apps/gfprm</URL>
<APEXID></APEXID>
<MyRequestID>7219068</MyRequestID>
<AppName></AppName>
<StepOut>9 Dec 2015</StepOut>
<StepIn></StepIn>
<Requester>SAIKAT.MANNA@testing.COM</Requester>
<BusinessUnit>UA</BusinessUnit>
<PortfolioManager></PortfolioManager>
<Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
</domain>
<domain>
<URL>sww-epw3.testing.com/apps/gfprm</URL>
<APEXID></APEXID>
<MyRequestID>7219068</MyRequestID>
<AppName></AppName>
<StepOut>9 Dec 2015</StepOut>
<StepIn></StepIn>
<Requester>SAIKAT.MANNA@testing.COM</Requester>
<BusinessUnit>UA</BusinessUnit>
<PortfolioManager></PortfolioManager>
<Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
</domain>
<domain>
<URL>sww-epw3.testing.com/apps/gfprm</URL>
<APEXID></APEXID>
<MyRequestID>7219068</MyRequestID>
<AppName></AppName>
<StepOut>9 Dec 2015</StepOut>
<StepIn></StepIn>
<Requester>SAIKAT.MANNA@testing.COM</Requester>
<BusinessUnit>UA</BusinessUnit>
<PortfolioManager></PortfolioManager>
<Remarks>Technical Assessment</Remarks> <APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
</domain></emie>
sww-epw3.testing.com/apps/gfprm
7219068
2015年12月9日
赛卡特。MANNA@testing.COM
UA
现场技术评估
sww-epw3.testing.com/apps/gfprm
7219068
2015年12月9日
赛卡特。MANNA@testing.COM
UA
现场技术评估
sww-epw3.testing.com/apps/gfprm
7219068
2015年12月9日
赛卡特。MANNA@testing.COM
UA
现场技术评估
我建议您将此作为起点:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/rules">
<xsl:variable name="site" select="emie/domain[1]/text()" />
<!-- first pass -->
<xsl:variable name="first-pass-rtf">
<xsl:for-each select="emie/domain/path">
<domain>
<URL>
<xsl:value-of select="concat($site, .)" />
</URL>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="@comment"/>
</xsl:call-template>
<APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
</domain>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="first-pass" select="exsl:node-set($first-pass-rtf)" />
<!-- output -->
<emie>
<xsl:apply-templates select="$first-pass"/>
</emie>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="';'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<xsl:element name="{substring-before($token, ':')}">
<xsl:value-of select="substring-after($token, ':')"/>
</xsl:element>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
补充: 我无法预测inout文件的令牌名称。因此我需要生成 在最终XML中具有统一元素的XML 要明确列出所需元素,请添加以下模板:
<xsl:template match="domain">
<xsl:copy>
<URL>
<xsl:value-of select="URL" />
</URL>
<APEXID>
<xsl:value-of select="APEXID" />
</APEXID>
<MyRequestID>
<xsl:value-of select="MyRequestID" />
</MyRequestID>
<AppName>
<xsl:value-of select="AppName" />
</AppName>
<StepOut>
<xsl:value-of select="StepOut" />
</StepOut>
<StepIn>
<xsl:value-of select="StepIn" />
</StepIn>
<Requester>
<xsl:value-of select="Requester" />
</Requester>
<BusinessUnit>
<xsl:value-of select="BusinessUnit" />
</BusinessUnit>
<PortfolioManager>
<xsl:value-of select="PortfolioManager" />
</PortfolioManager>
<Remarks>
<xsl:value-of select="Remarks" />
</Remarks>
<APEX_DATA_PORTAL_URL>
<xsl:value-of select="APEX_DATA_PORTAL_URL" />
</APEX_DATA_PORTAL_URL>
</xsl:copy>
</xsl:template>
我建议您将此作为起点: XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/rules">
<xsl:variable name="site" select="emie/domain[1]/text()" />
<!-- first pass -->
<xsl:variable name="first-pass-rtf">
<xsl:for-each select="emie/domain/path">
<domain>
<URL>
<xsl:value-of select="concat($site, .)" />
</URL>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="@comment"/>
</xsl:call-template>
<APEX_DATA_PORTAL_URL>somesite</APEX_DATA_PORTAL_URL>
</domain>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="first-pass" select="exsl:node-set($first-pass-rtf)" />
<!-- output -->
<emie>
<xsl:apply-templates select="$first-pass"/>
</emie>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="';'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<xsl:element name="{substring-before($token, ':')}">
<xsl:value-of select="substring-after($token, ':')"/>
</xsl:element>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
补充: 我无法预测inout文件的令牌名称。因此我需要生成 在最终XML中具有统一元素的XML 要明确列出所需元素,请添加以下模板:
<xsl:template match="domain">
<xsl:copy>
<URL>
<xsl:value-of select="URL" />
</URL>
<APEXID>
<xsl:value-of select="APEXID" />
</APEXID>
<MyRequestID>
<xsl:value-of select="MyRequestID" />
</MyRequestID>
<AppName>
<xsl:value-of select="AppName" />
</AppName>
<StepOut>
<xsl:value-of select="StepOut" />
</StepOut>
<StepIn>
<xsl:value-of select="StepIn" />
</StepIn>
<Requester>
<xsl:value-of select="Requester" />
</Requester>
<BusinessUnit>
<xsl:value-of select="BusinessUnit" />
</BusinessUnit>
<PortfolioManager>
<xsl:value-of select="PortfolioManager" />
</PortfolioManager>
<Remarks>
<xsl:value-of select="Remarks" />
</Remarks>
<APEX_DATA_PORTAL_URL>
<xsl:value-of select="APEX_DATA_PORTAL_URL" />
</APEX_DATA_PORTAL_URL>
</xsl:copy>
</xsl:template>
您的预期输出是什么?为什么您认为它需要两次处理?我想在输出1中再添加几个标记,并需要将日期格式化为dd/mm/yyysww-epw3.testing.com/apps/gfprm 7219068 2015年12月9日SAIKAT。MANNA@testing.COMUA技术评估somesite`请不要在评论中发布代码-改为编辑您的问题。-另请参见:。您发布的XSLT不会生成声明的输出;它会导致错误(元素名称不是有效的QName)。我对你有困难