使用XSL在第一个位置插入XML节点(Insert XML node at first position using XSL)

编程入门 行业动态 更新时间:2024-10-24 15:12:37
使用XSL在第一个位置插入XML节点(Insert XML node at first position using XSL)

如果导入不存在,XSLT将当前插入导入

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> ... ... <Import Project="$(SolutionDir)BuildShared.targets" /> </Project>

我需要它作为第一个节点插入它

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(SolutionDir)BuildShared.targets" /> ... ... </Project>

template.xsl;

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> </xsl:template> <xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> <Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

交换导入和apply-templates行给出;

runtime error: file template.xsl line 9 element copy

Attribute nodes must be added before any child nodes to an element.

The XSLT will currently insert the import if it doesn't exist

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> ... ... <Import Project="$(SolutionDir)BuildShared.targets" /> </Project>

I need it to insert it as the first node

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(SolutionDir)BuildShared.targets" /> ... ... </Project>

template.xsl;

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> </xsl:template> <xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> <Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

Swapping the import and apply-templates lines gives;

runtime error: file template.xsl line 9 element copy

Attribute nodes must be added before any child nodes to an element.

最满意答案

只需为node()和@*分别执行xsl:apply-templates :

<xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]"> <xsl:copy> <xsl:apply-templates select="@*"/> <Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template>

Just do your xsl:apply-templates for node() and @* separately:

<xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]"> <xsl:copy> <xsl:apply-templates select="@*"/> <Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template>

更多推荐

xsl,<xsl,template,电脑培训,计算机培训,IT培训"/> <meta name="descript

本文发布于:2023-08-03 01:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   位置   XML   XSL   node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!