使用参数匹配元素 inx XSLT [英] Using Parameters to match elements inx XSLT

查看:16
本文介绍了使用参数匹配元素 inx XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一篇文章中得到了以下模板..

I got the following template from another post..

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

<xsl:param name="pUncertainElName" select="'second'"/>
<xsl:param name="pParentPath" select="'outerElement/innerElement'" />
<xsl:param name="pOrderedNames" select="'|first|second|third|'"/>

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

<xsl:template match="outerElement/innerElement">
  <xsl:variable name="vrtfFirstPass">
      <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
      <xsl:apply-templates select=
        "self::*[not(*[name() = $pUncertainElName])
                or
                *[name()=$pUncertainElName and @missing-cause]]"
        mode="missing"/>
      </xsl:copy>
  </xsl:variable>

  <xsl:apply-templates select="ext:node-set($vrtfFirstPass)/*" mode="pass2"/>
</xsl:template>

<xsl:template match="*[@missing-cause]"/>

<xsl:template match="*" mode="missing">
    <xsl:element name="{$pUncertainElName}">
        <CharacterString>INSERTED BY TEMPLATE</CharacterString>
    </xsl:element>
</xsl:template>

<xsl:template match="outerElement/innerElement" mode="pass2">
  <xsl:copy>
  <xsl:apply-templates>
    <xsl:sort data-type="number" select=
    "string-length(substring-before($pOrderedNames,
                                    concat('|', name(), '|')
                                    )
                  )"/>
  </xsl:apply-templates>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

目的是添加缺失的元素,添加源文档中缺失的特定位置.源文件看起来像这样

The purpose is to add missing Elements add specific places that are missing in the source document. The sourcedocument looks like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doc>
  <outerElement>
    <innerElement>
      <first>
        <textElement>Some Text</textElement>
      </first>
      <second missing-cause="bla" />
      <third>
        <textElement>Some Text</textElement>
      </third>
    </innerElement>
  </outerElement>
</doc>

我必须以类似的方式添加很多这样的元素,所以我想使用参数来指定父路径和要插入的元素.

I have to add a lot of elements like this in a similar way so I want to use parameters to specify the parents path and the element I want to insert.

那么第一个问题来了:如何在匹配中使用参数?match="$parameter" 或变体似乎不起作用.

So here comes the first question: How do I use a parameter in a match? match="$parameter" or variants do not seem to work.

第二个:使用这个模板添加元素仍然存在问题,我认为它来自第二遍.

And the second one: There is still a problem with adding the element with this template which i think comes from the second pass.

如果我的文档看起来像张贴在上面,它会将输出展平为

If my document does look like posted above it flattens the output to

<doc>
  <outerElement>Some TextSome TextINSERTED BY TEMPLATE</outerElement>
</doc>

如果缺少它应该的工作.在第二条路径的构建中最有可能丢失了一些东西,但我不知道如何解决这个问题.

if the is missing its working as it should. There is missing something most likely in the buildup of the second path but i can't figure out how to fix this.

最后一个.. 可以在单个文档上使用不同的参数调用这个模板(例如 20 次)来转换它,还是应该尝试其他方法?

And the last.. Is it ok to call this template with different parameters like 20 times on a single document to transform it or should i try something else?

再次感谢您的帮助,对不起,我是新手;)

Thanks for help again and sorry I am new to this ;)

推荐答案

在 XSLT 1.0 和 XSLT 2.0 中,都不可能动态计算 XPath 表达式.

In both XSLT 1.0 and XSLT 2.0 it isn't possible to evaluate an XPath expression dynamically.

因此,您尝试使用 $pParentPath 执行的操作不会产生预期的结果.

Therefore, what you attempt to do with $pParentPath will not produce the desired result.

作为一种解决方法,您可以传递两个不同的参数:pchildNamepgrandchildName 并使用如下所示的内容:

As a workaround you may pass two different parameters: pchildName and pgrandchildName and use something like this:

*[name()=$pchildName]/*[name()=$pgrandchildName]

在 XSLT 1.0 中,在匹配模式中禁止引用变量或参数.在 XSLT 2.0 中是可以的.

In XSLT 1.0 a variable or parameter reference is forbidden in a match pattern. In XSLT 2.0 it is OK.

以下是转换,已更正以处理此特定 XML 文档:

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

    <xsl:param name="pUncertainElName" select="'second'"/>
    <xsl:param name="pOrderedNames" select="'|first|second|third|'"/>

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

    <xsl:template match="innerElement">
      <xsl:variable name="vrtfFirstPass">
          <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
          <xsl:apply-templates select=
            "self::*[not(*[name() = $pUncertainElName])
                    or
                    *[name()=$pUncertainElName and @missing-cause]]"
            mode="missing"/>
          </xsl:copy>
      </xsl:variable>

      <xsl:apply-templates select="ext:node-set($vrtfFirstPass)/*" mode="pass2"/>
    </xsl:template>

    <xsl:template match="*[@missing-cause]"/>

    <xsl:template match="*" mode="missing">
        <xsl:element name="{$pUncertainElName}">
            <CharacterString>INSERTED BY TEMPLATE</CharacterString>
        </xsl:element>
    </xsl:template>

    <xsl:template match="innerElement" mode="pass2">
      <xsl:copy>
      <xsl:apply-templates>
        <xsl:sort data-type="number" select=
        "string-length(substring-before($pOrderedNames,
                                        concat('|', name(), '|')
                                        )
                      )"/>
      </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<doc>
    <outerElement>
        <innerElement>
            <first>
                <textElement>Some Text</textElement>
            </first>
            <second missing-cause="bla" />
            <third>
                <textElement>Some Text</textElement>
            </third>
        </innerElement>
    </outerElement>
</doc>

产生了想要的、正确的结果:

<doc>
   <outerElement>
      <innerElement>
         <first>
            <textElement>Some Text</textElement>
         </first>
         <second>
            <CharacterString>INSERTED BY TEMPLATE</CharacterString>
         </second>
         <third>
            <textElement>Some Text</textElement>
         </third>
      </innerElement>
   </outerElement>
</doc>

可以修改转换,以便处理文档层次结构不同位置的不同元素的子元素:

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

    <xsl:param name="pUncertainElName" select="'second'"/>
    <xsl:param name="pOrderedNames" select="'|first|second|third|'"/>

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

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

      <xsl:apply-templates select="ext:node-set($vrtfFirstPass)/*" mode="pass2"/>
    </xsl:template>

    <xsl:template match="innerElement">
      <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
          <xsl:apply-templates select=
            "self::*[not(*[name() = $pUncertainElName])
                    or
                    *[name()=$pUncertainElName and @missing-cause]]"
            mode="missing"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[@missing-cause]"/>

    <xsl:template match="*" mode="missing">
        <xsl:element name="{$pUncertainElName}">
            <CharacterString>INSERTED BY TEMPLATE</CharacterString>
        </xsl:element>
    </xsl:template>

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

    <xsl:template match="innerElement" mode="pass2">
      <xsl:copy>
       <xsl:apply-templates>
         <xsl:sort data-type="number" select=
        "string-length(substring-before($pOrderedNames,
                                        concat('|', name(), '|')
                                        )
                      )"/>
       </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时(包含两个 innerElement 元素——具有不同的父元素和不同的深度——它们的子元素需要专门的处理):

When this transformation is applied on the following XML document (containing two innerElement elements -- with different parents and at different depths -- whose children need the specialized processing):

<doc>
    <outerElement>
        <innerElement>
            <first>
                <textElement>Some Text</textElement>
            </first>
            <second missing-cause="bla" />
            <third>
                <textElement>Some Text</textElement>
            </third>
        </innerElement>
        <outerElement2>
          <outerElement3>
            <innerElement>
                    <first>
                        <textElement>Some Text</textElement>
                    </first>
                    <third>
                        <textElement>Some Text</textElement>
                    </third>
            </innerElement>
          </outerElement3>
        </outerElement2>
    </outerElement>
</doc>

产生想要的、正确的结果:

<doc>
   <outerElement>
      <innerElement>
         <first>
            <textElement>Some Text</textElement>
         </first>
         <second>
            <CharacterString>INSERTED BY TEMPLATE</CharacterString>
         </second>
         <third>
            <textElement>Some Text</textElement>
         </third>
      </innerElement>
      <outerElement2>
         <outerElement3>
            <innerElement>
               <first>
                  <textElement>Some Text</textElement>
               </first>
               <second>
                  <CharacterString>INSERTED BY TEMPLATE</CharacterString>
               </second>
               <third>
                  <textElement>Some Text</textElement>
               </third>
            </innerElement>
         </outerElement3>
      </outerElement2>
   </outerElement>
</doc>

最后,我们可以进一步修改转换,以便它可以处理不同命名的父级的子级——比如 innerElementsomeOtherInnerElement:

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

    <xsl:param name="pUncertainElName" select="'second'"/>
    <xsl:param name="pOrderedNames" select="'|first|second|third|'"/>

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

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

      <xsl:apply-templates select="ext:node-set($vrtfFirstPass)/*" mode="pass2"/>
    </xsl:template>

    <xsl:template match="innerElement | someOtherInnerElement">
      <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
          <xsl:apply-templates select=
            "self::*[not(*[name() = $pUncertainElName])
                    or
                    *[name()=$pUncertainElName and @missing-cause]]"
            mode="missing"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[@missing-cause]"/>

    <xsl:template match="*" mode="missing">
        <xsl:element name="{$pUncertainElName}">
            <CharacterString>INSERTED BY TEMPLATE</CharacterString>
        </xsl:element>
    </xsl:template>

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

    <xsl:template match="innerElement | someOtherInnerElement" mode="pass2">
      <xsl:copy>
       <xsl:apply-templates>
         <xsl:sort data-type="number" select=
        "string-length(substring-before($pOrderedNames,
                                        concat('|', name(), '|')
                                        )
                      )"/>
       </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时,其中以所需方式处理的子级具有以这两个名称命名的父级(innerElementsomeOtherInnerElement):

When this transformation is applied on the following XML document, where the children to be processed in the required way have parents named with these two names ( innerElement and someOtherInnerElement):

<doc>
    <outerElement>
        <innerElement>
            <first>
                <textElement>Some Text</textElement>
            </first>
            <second missing-cause="bla" />
            <third>
                <textElement>Some Text</textElement>
            </third>
        </innerElement>
        <outerElement2>
          <outerElement3>
            <someOtherInnerElement>
                    <first>
                        <textElement>Some Text</textElement>
                    </first>
                    <third>
                        <textElement>Some Text</textElement>
                    </third>
            </someOtherInnerElement>
          </outerElement3>
        </outerElement2>
    </outerElement>
</doc>

再次产生想要的、正确的结果:

<doc>
   <outerElement>
      <innerElement>
         <first>
            <textElement>Some Text</textElement>
         </first>
         <second>
            <CharacterString>INSERTED BY TEMPLATE</CharacterString>
         </second>
         <third>
            <textElement>Some Text</textElement>
         </third>
      </innerElement>
      <outerElement2>
         <outerElement3>
            <someOtherInnerElement>
               <first>
                  <textElement>Some Text</textElement>
               </first>
               <second>
                  <CharacterString>INSERTED BY TEMPLATE</CharacterString>
               </second>
               <third>
                  <textElement>Some Text</textElement>
               </third>
            </someOtherInnerElement>
         </outerElement3>
      </outerElement2>
   </outerElement>
</doc>

说明:

这与上一个问题的逻辑基本相同:

This is essentially the same logic as for the previous question:

  1. 两次处理.

  1. Two-pass processing.

覆盖身份规则.

正确使用模板和模板匹配模式.

Proper use of templates and template match-patterns.

按元素名称的首选顺序对元素进行排序.

Sorting elements by the preferred order of their names.

这篇关于使用参数匹配元素 inx XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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