简体   繁体   English

使用xslt 1.0替换xml中的属性值中的字符串

[英]Replace a string in attribute value in a xml using xslt 1.0

I am a novice in XSLT. 我是XSLT的新手。 I have been trying to transform a XML for a longtime but unable to do. 很长时间以来,我一直在尝试转换XML,但是无法做到。 The input XML is like this: 输入的XML如下所示:

<Load v:MajorVersion="05" v:MinorVersion="01" xmlns="version 2" xmlns:v="version 2">
  <annotation/>
  <Header></Header>
  <Body></Body>
</Load>

The output should replace the value of attributes xmlns and xmlns:v from version 2 to version 1 if its value is version 2 , otherwise it should be left as is. 如果输出的值为version 2 ,则输出应将属性xmlnsxmlns:vversion 2替换为version 1 ,否则应保留原样。
The problem is that the attributes are prone to change, and the input xml could be like xmlns:n , or xmlns:m , or these attributes may not even exist. 问题在于属性易于更改,并且输入xml可能类似于xmlns:nxmlns:m ,或者这些属性甚至可能不存在。

Finally we should search for all attributes of node Load and search for string version 2 in all the attributes (like xmlns, xmlns:v, xmlns:n, etc) and replace by version 1 . 最后,我们应该搜索节点Load所有属性,并在所有属性(例如xmlns,xmlns:v,xmlns:n等)中搜索字符串version 2并替换为version 1

Please help me. 请帮我。 Trust me I have spent hours finding an answer for this. 相信我,我已经花了数小时来寻找答案。

your attributes are namespace declaration. 您的属性是名称空间声明。 Therefore have a look for "How To Change the URL of a Namespace". 因此,请查找“如何更改命名空间的URL”。

You may try something like this: 您可以尝试如下操作:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:v2="version 2">
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
          <xsl:copy>
              <xsl:apply-templates select="node()"/>
          </xsl:copy>  
    </xsl:template>

    <xsl:template match="v2:*">
        <xsl:element name="{local-name()}" xmlns="version 1">
              <xsl:apply-templates select="node()|@*"/>
        </xsl:element>  
  </xsl:template>
  <xsl:template match="@v2:*">
    <xsl:attribute name="{local-name()}" xmlns="version 1">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet

You need to think in terms of the XPath data model, not in terms of the source XML. 您需要考虑XPath数据模型,而不要考虑源XML。 If you understand the data model for your XML fragment, you will appreciate that you don't want to change the value of the xmlns attribute, you want to change the namespace part of the element and attribute names; 如果您了解XML片段的数据模型,那么您将不希望更改xmlns属性的值,而希望更改元素和属性名称的名称空间部分; that is, you want to create new elements/attributes whose names have the same local name but different namespace URIs from the old elements/attributes. 也就是说,您要创建名称与本地元素/属性相同但名称空间URI与旧元素/属性不同的新元素/属性。 So you want instructions like 所以你想要像这样的指示

<xsl:element name="name()" namespace="...."/>
<xsl:attribute name="v:majorVersion" namespace="...."/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM