如何仅使用XSLT转换XML文件的属性,而保留其他内容?

发布于 2024-09-10 18:31:02 字数 1218 浏览 4 评论 0原文

我有一个如下的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-09-17 18:31:02

此样式表:

<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 将属性添加到结果树。

This stylesheet:

<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>

Result:

<DM>
    <DV id="From SQL!">
        <Sample aid="From SQL!"></Sample>
    </DV>
    <DV id="From SQL!">
        <Sample aid="From SQL!"></Sample>
    </DV>
</DM>

Note: Don't need to break "identity transform". Add attributes to result tree with xsl:attribute.

一口甜 2024-09-17 18:31:02

好吧,您正在使用一个模板来匹配节点和属性。使用两个单独的模板可以更轻松地区分它们:

<!-- 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>

要确定字符串是否以特定子字符串开头,您需要使用 starts-with() 函数。你可以这样使用它:

<xsl:if test="starts-with(.,'SQL:')">
  <!-- The current node starts with "SQL:" -->
</xsl:if>

Well, you're using one template to match both nodes and attributes. It would be easier to distinguish between them using two separate templates:

<!-- 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>

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:

<xsl:if test="starts-with(.,'SQL:')">
  <!-- The current node starts with "SQL:" -->
</xsl:if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文