1

我正在尝试在 XSLT 1.0 中执行以下操作:

将所有 Fram 元素原样复制到输出中,其中条件是 Fram 的任何属性(@name AND @type AND @ref)值与任何 XFram 元素匹配,然后它应该使用 XFram/text() 更新 Fram/text() . 之后复制 XFram 元素的其余部分(所有这些 dint 都符合上述条件)并将它们转换为 Fram 标签。

注意:第三个 Fram 元素的 all 属性与第三个 Xfram 元素匹配,因此在第三个 Fram 元素中附加了文本。XFarm 元素的其余部分转换为 Fram 元素并添加到最后一个 Fram 元素之后。您还会注意到 Fram 的顺序没有改变。

输入.XML

<Doc>AL
<Frams>
 <Fram type="x" name="Fram1" ref="ref1">This is Fram One</Fram>
 <Fram type="y" name="Fram2" ref="ref2">This is Fram Two</Fram>
 <Fram type="z" name="Fram3" ref="ref3">This is Fram Three</Fram>
 <Fram type="a" name="Fram4" ref="ref3">This is Fram Four</Fram>
 <Fram type="b" name="Fram5" ref="ref3">This is Fram Five</Fram>
</Frams>
<XFram>
 <XFram type="e" name="XFram1" ref="Xref1">This is XFram One</Fram>
 <XFram type="f" name="XFram2" ref="Xref2">This is XFram Two</Fram>
 <XFram type="z" name="XFram3" ref="Xref3">This is XFram Three</Fram>
 <XFram type="e" name="XFram1" ref="Xref1">This is XFram Four</Fram>
<XFram>
<Doc>

输出应该是:

<Doc>
 <Frams>
 <Fram type="x" name="Fram1" ref="ref1">This is Fram One</Fram>
 <Fram type="y" name="Fram2" ref="ref2">This is Fram Two</Fram>
 <Fram type="z" name="Fram3" ref="ref3">This is XFram Three</Fram>
 <Fram type="a" name="Fram4" ref="ref3">This is Fram Four</Fram>
 <Fram type="b" name="Fram5" ref="ref3">This is Fram Five</Fram>
 <XFram type="e" name="XFram1" ref="Xref1">This is XFram One</Fram>
 <XFram type="f" name="XFram2" ref="Xref2">This is XFram Two</Fram>
 <XFram type="e" name="XFram1" ref="Xref1">This is XFram Four</Fram>
 </Frams>
<Doc>

我正在做这样的事情,但无法想到逻辑:

<xsl:template match="/">
    <xsl:for-each select="XFram">
        <xsl:variable name="type">
            <xsl:value-of select="type"/>
        </xsl:variable>
        <xsl:variable name="name">
            <xsl:value-of select="name"/>
        </xsl:variable>
        <xsl:variable name="ref">
            <xsl:value-of select="ref"/>
        </xsl:variable>
        <xsl:for-each select="//Fram">
            <xsl:choose>
                <xsl:when test="(type = $type) and (name = $name) and (ref = $ref)"> </xsl:when>
            </xsl:choose>
        </xsl:for-each>           
    </xsl:for-each>
</xsl:template>
4

1 回答 1

0

这种转变

<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()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="Fram[last()]">
  <xsl:call-template name="identity"/>

  <xsl:apply-templates
       select="../../XFram/*"/>
 </xsl:template>

 <xsl:template match="XFram/XFram">
  <xsl:if test=
   "not(/*/Frams/*
             [@type = current()/@type
            and
              concat('X',@name) = current()/@name
            and
              concat('X',@ref) = current()/@ref
             ]
         )">
     <xsl:copy-of select="."/>
  </xsl:if>
 </xsl:template>
 <xsl:template match="XFram"/>
</xsl:stylesheet>

当应用于提供的 XML 文档时(已纠正严重的格式错误!!!):

<Doc>
    <Frams>
        <Fram type="x" name="Fram1" ref="ref1">This is Fram One</Fram>
        <Fram type="y" name="Fram2" ref="ref2">This is Fram Two</Fram>
        <Fram type="z" name="Fram3" ref="ref3">This is Fram Three</Fram>
        <Fram type="a" name="Fram4" ref="ref3">This is Fram Four</Fram>
        <Fram type="b" name="Fram5" ref="ref3">This is Fram Five</Fram>
    </Frams>
    <XFram>
        <XFram type="e" name="XFram1" ref="Xref1">This is XFram One</XFram>
        <XFram type="f" name="XFram2" ref="Xref2">This is XFram Two</XFram>
        <XFram type="z" name="XFram3" ref="Xref3">This is XFram Three</XFram>
        <XFram type="e" name="XFram1" ref="Xref1">This is XFram Four</XFram></XFram>
</Doc>

产生想要的正确结果:

<Doc>
   <Frams>
      <Fram type="x" name="Fram1" ref="ref1">This is Fram One</Fram>
      <Fram type="y" name="Fram2" ref="ref2">This is Fram Two</Fram>
      <Fram type="z" name="Fram3" ref="ref3">This is Fram Three</Fram>
      <Fram type="a" name="Fram4" ref="ref3">This is Fram Four</Fram>
      <Fram type="b" name="Fram5" ref="ref3">This is Fram Five</Fram>
      <XFram type="e" name="XFram1" ref="Xref1">This is XFram One</XFram>
      <XFram type="f" name="XFram2" ref="Xref2">This is XFram Two</XFram>
      <XFram type="e" name="XFram1" ref="Xref1">This is XFram Four</XFram>
   </Frams>
</Doc>
于 2012-04-21T02:41:47.283 回答