使用 [MS] XSLT 脚本修改 XML 节点 [英] Modify an XML node using [MS] XSLT Script

查看:42
本文介绍了使用 [MS] XSLT 脚本修改 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个节点并使用xsl:脚本函数.此外,匹配该节点的子节点的模板应该仍然执行他们的工作(在脚本处理完节点之后).

I would like to select a node and modify its attributes and child-nodes using an xsl:script function. In addition, templates matching child-nodes of that node should STILL perform their job (after script is done processing the node).

  1. 可以使用 XSLT 来完成吗?
  2. 您能否为这种转换提供一个示例/框架?

推荐答案

是的,可以做到.我似乎没有看到问题是什么,因为 XSL 脚本的 XML(或任何输出)是独立于其输入进行缓冲的.

Yes, it can be done. I don't seem to see what the problem is because the XML (or whatever output) of an XSL script is buffered independently from its input.

以下示例说明了这一点,其中一个简单的 XSL 脚本主要按原样复制输入 XML 文档,并更改了一些内容:

This is illustrated in the following example whereby a simple XSL script copies an input XML document mostly as-is, changing a few things:

  • 根元素名称和属性
  • 通过从层次结构中删除元素来展平
  • 删除结果/日期元素
  • 将商品的来源"属性重命名为来源"
  • 更改项目的级别"属性值
  • 重命名 item 元素的 FirstName 和 LastName 元素

样本输入

<?xml version="1.0" encoding="ISO-8859-1"?>
<MyRoot version="1.2">
    <results>
        <info>Alpha Bravo</info>
        <author>Employee No 321</author>
        <date/>
        <item source="www" level="6" cost="33">
            <FirstName>Jack</FirstName>
            <LastName>Frost</LastName>
            <Date>1998-10-30</Date>
            <Organization>Lemon growers association</Organization>
         </item>
         <item source="db-11" level="1" cost="65" qry="routine 21">
            <FirstName>Mike</FirstName>
            <LastName>Black</LastName>
            <Date>2006-10-30</Date>
            <Organization>Ford Motor Company</Organization>
         </item>
    </results>
</MyRoot>

产出

<?xml version="1.0" encoding="utf-16"?>
<MyNewRoot version="0.1">
    <author>Employee No 321</author>
    <info>Alpha Bravo</info>
    <item cost="33" origin="www" level="77">
        <GivenName>Jack</GivenName>
        <FamilyName>Frost</FamilyName>
        <Date>1998-10-30</Date>
        <Organization>Lemon growers association</Organization>
    </item>
    <item cost="65" qry="routine 21" origin="db-11" level="77">
        <GivenName>Mike</GivenName>
        <FamilyName>Black</FamilyName>
        <Date>2006-10-30</Date>
        <Organization>Ford Motor Company</Organization>
    </item>
</MyNewRoot>

XSL 脚本

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="#default">

<xsl:template match="MyRoot">
   <xsl:call-template name="MainTemplate">
   </xsl:call-template>
</xsl:template>

<xsl:template name="MainTemplate">
   <MyNewRoot version="0.1">

   <xsl:copy-of select="results/author" />
   <xsl:copy-of select="results/info" />

   <xsl:for-each select="results/item">
      <xsl:call-template name="FixItemElement"/>
   </xsl:for-each>

  </MyNewRoot> 
</xsl:template>

<xsl:template name="FixItemElement">
    <xsl:copy>
        <xsl:copy-of select="@*[not(name()='source' or name()='level')]" />
        <xsl:attribute name="origin">
            <xsl:value-of select="@source"/>
        </xsl:attribute>
        <xsl:attribute name="level">
            <xsl:value-of select="77"/>
        </xsl:attribute>

        <xsl:for-each select="descendant::*">
          <xsl:choose>
            <xsl:when test="local-name(.) = 'FirstName'">
                <GivenName>
                   <xsl:value-of select="."/>
                </GivenName>
            </xsl:when>
            <xsl:when test="local-name(.) = 'LastName'">
                 <FamilyName>
                   <xsl:value-of select="."/>
                </FamilyName>
            </xsl:when>
            <xsl:otherwise>
              <xsl:copy>
                 <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
            </xsl:otherwise>
          </xsl:choose>       
        </xsl:for-each>       
    </xsl:copy>
</xsl:template> 

这篇关于使用 [MS] XSLT 脚本修改 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆