繁体   English   中英

使用 xslt 替换 xml 中的值

[英]replace value in xml using xslt

<Person>
<AdditionalAttributes groupLabel="Profile">
<AdditionalAttribute name="First Name" value="John"/>
<AdditionalAttribute name="Last Name" value="Smith"/>
</AdditionalAttributes>

<AdditionalAttributes groupLabel="Additional">
<AdditionalAttribute name="email" value="John Smith(jsmith)"/>
<AdditionalAttribute name="Created Date" value="2016-04-20T19:50:01Z"/>
</AdditionalAttributes>
</Person>

你能告诉我如何使用 xslt 将 @gmail.com 添加到从 John Smith (jsmith) 到 John Smith(jsmith**@gmail.com**) 的元素电子邮件的值,假设电子邮件值是动态的

感谢致敬

XSLT 1.0 ,可以使用translateconcat ,不需要xslt ,只需使用普通的xpath

//Person/AdditionalAttributes/AdditionalAttribute[@name='email']/concat(translate(@value, ')', '@'), 'gmail.com)')

尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="AdditionalAttribute[@name='email']/@value">
    <xsl:attribute name="value">
        <xsl:value-of select="substring-before(., ')')"/>
        <xsl:text>@gmail.com)</xsl:text>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

这是假设除最后一个外没有右括号。 或者,您可以这样做:

<xsl:template match="AdditionalAttribute[@name='email']/@value">
    <xsl:attribute name="value">
        <xsl:value-of select="substring(., 1, string-length(.) - 1)"/>
        <xsl:text>@gmail.com)</xsl:text>
    </xsl:attribute>
</xsl:template>

添加:

您还可以告诉我如何用“jsmith@gmail.com”替换“John Smith(jsmith)”吗?

提取括号中的文本并将域名附加到其中:

<xsl:template match="AdditionalAttribute[@name='email']/@value">
    <xsl:attribute name="value">
        <xsl:value-of select="substring-before(substring-after(., '('), ')')"/>
        <xsl:text>@gmail.com</xsl:text>
    </xsl:attribute>
</xsl:template>

一、XSLT 1.0

类似于 michael.hor257k 的解决方案,但使用AVT s -- 属性值模板。

因此不需要<xsl:attribute>运算符

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="AdditionalAttribute[@name='email']">
    <AdditionalAttribute name="email"
        value="{substring-before(@value, '(')
                }({substring-before(
                             substring-after(@value,'('),
                             ')'
                                    )}@gmail.com)">
      <xsl:apply-templates select="@*[not(name()='value')]"/>
    </AdditionalAttribute>
  </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<Person>
    <AdditionalAttributes groupLabel="Profile">
        <AdditionalAttribute name="First Name" value="John"/>
        <AdditionalAttribute name="Last Name" value="Smith"/>
    </AdditionalAttributes>
    <AdditionalAttributes groupLabel="Additional">
        <AdditionalAttribute name="email" value="John Smith(jsmith)"/>
        <AdditionalAttribute name="Created Date" value="2016-04-20T19:50:01Z"/>
    </AdditionalAttributes>
</Person>

产生了想要的正确结果

<Person>
   <AdditionalAttributes groupLabel="Profile">
      <AdditionalAttribute name="First Name" value="John"/>
      <AdditionalAttribute name="Last Name" value="Smith"/>
   </AdditionalAttributes>
   <AdditionalAttributes groupLabel="Additional">
      <AdditionalAttribute name="email" value="John Smith(jsmith@gmail.com)"/>
      <AdditionalAttribute name="Created Date" value="2016-04-20T19:50:01Z"/>
   </AdditionalAttributes>
</Person>

二。 XSLT 2.0

此转换再次使用 AVT 和 XPath 2.0 replace()函数:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="AdditionalAttribute[@name='email']">
    <AdditionalAttribute name="email"
        value="{replace(@value, '\)', '@gmail.com)')}">
      <xsl:apply-templates select="@*[not(name()='value')]"/>
    </AdditionalAttribute>
  </xsl:template>
</xsl:stylesheet>

暂无
暂无

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

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