Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 每个循环的相同循环中的搜索结果_Loops_Xslt - Fatal编程技术网

Loops 每个循环的相同循环中的搜索结果

Loops 每个循环的相同循环中的搜索结果,loops,xslt,Loops,Xslt,我正在使用XSLT1.0。在地址节点中循环时,我需要读取当前节点并从下一个节点检索数据。是否可以使用我使用的代码执行此操作,还是需要在循环中执行循环?好心的建议。非常感谢 我使用的代码是: <Data> <xsl:for-each select="Record/Address> <xsl:if test="Objtyp='H'"> <xsl:variable name="objkeyH" select="Objkey"/> <Loc

我正在使用XSLT1.0。在地址节点中循环时,我需要读取当前节点并从下一个节点检索数据。是否可以使用我使用的代码执行此操作,还是需要在循环中执行循环?好心的建议。非常感谢

我使用的代码是:

<Data>
 <xsl:for-each select="Record/Address>
 <xsl:if test="Objtyp='H'">
 <xsl:variable name="objkeyH" select="Objkey"/>
  <Location>
    <Region><xsl:value-of select="description"/></Region>
    <houseNumber><xsl:value-of select="houseNumber[parentID=$objkeyH]"/></houseNumber>
    <Street><xsl:value-of select="Street[parentID=$objkeyH]"/></Street>
    <Postcode><xsl:value-of select="Postcode[parentID=$objkeyH]"/></Postcode>
    <City><xsl:value-of select="City[parentID=$objkeyH]"/></City>     
  </Location>
 <xsl:if>
 </xsl:for-each>
</Data>


首先,最好从基于模板的方法开始。然后,您将拥有一个模板,该模板将地址记录与Objtype='H'

<xsl:template match="Address[Objtyp='H']">
   <Location>
      ....

....
您还需要一个模板来匹配其他地址记录,以便忽略它们。这样的模板的优先级比上面更具体的模板低

<xsl:template match="Address" />

就从另一个节点检索数据而言,键通常是一种更好的方法,因此在您的情况下,您可以定义一个键,以根据记录的parentID值查找地址

<xsl:key name="child" match="Address" use="parentID" />

然后可以检索其他值,如下所示:

<xsl:apply-templates select="key('child', Objkey)/houseNumber" />

这还有一个优点,即如果相关记录中不存在houseNumber,则不会输出任何内容(与输出空的houseNumber元素相反)

最后,要将根记录节点重命名为数据,您还需要一个模板

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

试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:key name="child" match="Address" use="parentID" />

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

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

  <xsl:template match="Address[Objtyp='H']">
    <Location>
      <Region><xsl:value-of select="description"/></Region>
      <xsl:apply-templates select="key('child', Objkey)/houseNumber" />
      <xsl:apply-templates select="key('child', Objkey)/Street" />
      <xsl:apply-templates select="key('child', Objkey)/Postcode" />
      <xsl:apply-templates select="key('child', Objkey)/City" />
    </Location>
  </xsl:template>

  <xsl:template match="Address" />
</xsl:stylesheet>


请注意使用复制现有节点。

这取决于H型父级是否可以有多个D型(或其他类型)子级。如果它们存在,你想用它们做什么。只有一个类型D,它总是在一个类型H之后。如果在类型H之后没有类型D,那么只需在。如果在类型H之后有类型D,则在中输出两个结果。类型D也可以通过字段D-parentID链接到类型H,字段D-parentID是类型H-OBJKEY,那么这里的问题到底是什么?您的代码可以工作(或者说,如果您修复了拼写错误,您的代码也可以工作)。依我拙见使用一个键来获取子数据会更有效,因为这显然是XML作者的意图——但这不是必需的。非常感谢Tim提供的详细反馈。你的方法很有效。但是,通过使用模板匹配,它复制了输入结构。我正试图改变这一点,但我无法改变输入结构。你能告诉我怎样才能换成正确的结构吗?啊,是的。对不起,我错过了记录已更改为数据的事实。但这对于模板方法来说仍然是直截了当的。我相应地改变了我的答案。我明白了。我试图将记录更改为数据。再次感谢蒂姆的帮助。
  <xsl:template match="Record">
    <Data>
      <xsl:apply-templates select="@*|node()"/>
    </Data>
  </xsl:template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:key name="child" match="Address" use="parentID" />

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

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

  <xsl:template match="Address[Objtyp='H']">
    <Location>
      <Region><xsl:value-of select="description"/></Region>
      <xsl:apply-templates select="key('child', Objkey)/houseNumber" />
      <xsl:apply-templates select="key('child', Objkey)/Street" />
      <xsl:apply-templates select="key('child', Objkey)/Postcode" />
      <xsl:apply-templates select="key('child', Objkey)/City" />
    </Location>
  </xsl:template>

  <xsl:template match="Address" />
</xsl:stylesheet>