每个逻辑中的每一个

时间:2012-04-20 21:48:11

标签: xslt xslt-1.0

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

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

注意:第三个Fram元素的all属性与第三个Xfram元素匹配,因此在Third Fram元素中附加了文本。其余的XFarm元素被转换为Fram元素并在最后一个Fram元素之后添加。你也会注意到Fram的订单没有改变。

Input.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>

1 个答案:

答案 0 :(得分: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>