Xslt 如何仅转换XML文件';s属性,并保留其他内容?
我有一个如下所示的xml文件,现在我想使用XSLT转换它,保留所有元素和属性,但是如果属性的值以“SQL:”开头,那么执行SQL并用解析的SQL替换属性值(它涉及到。现在我遇到了一个问题:如何检查当前节点类型是否为属性,以及如何替换属性值,我基于visual studio默认模板,如下所示: 示例xml文件(real中有许多元素):Xslt 如何仅转换XML文件';s属性,并保留其他内容?,xslt,msxsl,Xslt,Msxsl,我有一个如下所示的xml文件,现在我想使用XSLT转换它,保留所有元素和属性,但是如果属性的值以“SQL:”开头,那么执行SQL并用解析的SQL替换属性值(它涉及到。现在我遇到了一个问题:如何检查当前节点类型是否为属性,以及如何替换属性值,我基于visual studio默认模板,如下所示: 示例xml文件(real中有许多元素): 默认xslt: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version=
默认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>
好吧,您使用一个模板来匹配节点和属性。使用两个单独的模板更容易区分它们:
<!-- One template for nodes -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Another template for attributes -->
<xsl:template match="@*">
<!-- Special case for SQL attributes goes here -->
</xsl:template>
要确定字符串是否以特定子字符串开头,您需要使用该函数。您可以这样使用它:
<xsl:if test="starts-with(.,'SQL:')">
<!-- The current node starts with "SQL:" -->
</xsl:if>
好吧,您使用一个模板来匹配节点和属性。使用两个单独的模板更容易区分它们:
<!-- One template for nodes -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Another template for attributes -->
<xsl:template match="@*">
<!-- Special case for SQL attributes goes here -->
</xsl:template>
要确定字符串是否以特定子字符串开头,您需要使用该函数。您可以这样使用它:
<xsl:if test="starts-with(.,'SQL:')">
<!-- The current node starts with "SQL:" -->
</xsl:if>
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[starts-with(translate(substring(.,1,4),'sql','SQL'),'SQL:')]">
<xsl:attribute name="{name()}">
<xsl:value-of select="'From SQL!'"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
结果:
<DM>
<DV id="From SQL!">
<Sample aid="From SQL!"></Sample>
</DV>
<DV id="From SQL!">
<Sample aid="From SQL!"></Sample>
</DV>
</DM>
注意:不需要中断“身份转换”。使用xsl:attribute
此样式表向结果树添加属性:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[starts-with(translate(substring(.,1,4),'sql','SQL'),'SQL:')]">
<xsl:attribute name="{name()}">
<xsl:value-of select="'From SQL!'"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
结果:
<DM>
<DV id="From SQL!">
<Sample aid="From SQL!"></Sample>
</DV>
<DV id="From SQL!">
<Sample aid="From SQL!"></Sample>
</DV>
</DM>
注意:无需中断“身份转换”。使用
xsl:attribute
将属性添加到结果树很酷,但是我仍然不知道如何复制属性的名称,您能举个例子来复制属性的名称并用静态测试替换值吗?非常感谢!asfsadf现在似乎我可以使用.非常感谢。没有必要打破“一致性转换”。如果您需要以特殊方式处理其他属性,您会怎么做?更多xsl:if
?很酷,但我仍然不知道如何复制属性的名称,您能举个例子复制属性的名称并用静态测试替换值吗?非常感谢!asfsadf现在看起来像我可以使用“复制”。非常感谢。没有必要破坏“一致性转换”。如果需要以特殊方式处理其他属性,您会怎么做?更多xsl:if
?