简体   繁体   English

为XSLT Copy-Of编写模板以转换具有差异的消息。 怎么样?

[英]Writing a template for XSLT Copy-Of to transform a message with differences. How?

I have an xml message that is in the older schema (xsd) format. 我有一个旧的架构(xsd)格式的xml消息。 My new schema is exactly the same but I embedded an element inside the older one. 我的新架构完全相同,但我在旧版本中嵌入了一个元素。 For example : 例如 :

My old schema had an element : 我的旧架构有一个元素:

<exclude> MyRestriction </exclude>

but my new schema is like this : 但我的新架构是这样的:

<exclude> <restriction> MyRestriction </restriction> </exclude>

and the entire message is the same as before. 整个消息和以前一样。 Last time I used to do a copy-of but now I need to have a template that copy-of everything but move the value of the exclude to the restriction tag. 上次我曾经做过副本但是现在我需要一个模板复制所有内容,但是将exclude的值移动到限制标记。 Anyone can help me please ? 有人可以帮我吗?

Thanks 谢谢

You could use a template to match the text in an exclude template 您可以使用模板来匹配排除模板中的文本

<xsl:template match="exclude/text()">
   <restriction><xsl:value-of select="." /></restriction>
</xsl:template>

This way will keep any other child elements within exclude should they be required. 这样,将保留任何其他子元素中排除他们应该是必需的。

So, given the following XSLT 所以,给出以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="exclude/text()">
      <restriction><xsl:value-of select="." /></restriction>
   </xsl:template>

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

When applied to the following XML 应用于以下XML时

<exclude ex="1"> MyRestriction <test>Hello</test> </exclude>

The following is output 输出如下

<exclude ex="1">
   <restriction> MyRestriction </restriction>
   <test>Hello</test>
</exclude>

Use this template: 使用此模板:

<xsl:template match="exclude">
  <xsl:copy>
    <restriction>
      <xsl:value-of select="."/>
    </restriction>
  </xsl:copy>
</xsl:template>

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

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