如何仅使用XSLT转换XML文件的属性,而保留其他内容?
我有一个如下的xml文件,现在我想使用XSLT对其进行转换,保留所有元素和属性,但如果它发生在值以“SQL:”开头的属性上,则执行sql并替换已解析 SQL 的属性值(它涉及 http: //msdn.microsoft.com/en-us/library/533texsx(VS.90).aspx 现在我遇到了一个问题:如何检查当前节点类型是否为属性,以及如何替换该属性值,我基于Visual Studio默认模板如下:
示例xml文件(实际有很多元素):
<DM>
<DV id="SQL:Select something from db">
<Sample aid="SQL:Select something from db">
</Sample>
</DV>
<DV id="SQL:Select something from db">
<Sample aid="SQL:Select something from db">
</Sample>
</DV>
</DM>
默认xslt:
<?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"
xmlns:ms="urn:schemas-microsoft-com:xslt" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have a xml file as below, and now I want to use the XSLT to transformer it, keep all the elements and attributes, but if it happen to the attributes with the value started with "SQL:", then execute the sql and replace the attribute value with the resolved SQL(it involve the http://msdn.microsoft.com/en-us/library/533texsx(VS.90).aspx. now I encoutered the issue:how to check if the current node type is attribute, and how to replace the attribute value, I base on the visual studio default template as below:
the example xml file(there are many elements in real):
<DM>
<DV id="SQL:Select something from db">
<Sample aid="SQL:Select something from db">
</Sample>
</DV>
<DV id="SQL:Select something from db">
<Sample aid="SQL:Select something from db">
</Sample>
</DV>
</DM>
default xslt:
<?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"
xmlns:ms="urn:schemas-microsoft-com:xslt" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此样式表:
结果:
注意:不需要破坏“身份转换”。使用
xsl:attribute
将属性添加到结果树。This stylesheet:
Result:
Note: Don't need to break "identity transform". Add attributes to result tree with
xsl:attribute
.好吧,您正在使用一个模板来匹配节点和属性。使用两个单独的模板可以更轻松地区分它们:
要确定字符串是否以特定子字符串开头,您需要使用 starts-with() 函数。你可以这样使用它:
Well, you're using one template to match both nodes and attributes. It would be easier to distinguish between them using two separate templates:
To determine if a string starts with a particular substring, you'll want to use the starts-with() function. You can use it like this: