添加属性时从子节点XSLT获取三级父节点

添加属性时从子节点XSLT获取三级父节点,xslt,Xslt,XML 这个“@*| node()”是指二级父节点“foe”吗 不!这是指子节点和属性。 如何复制添加属性及其父级 在代码中,您通过说出来匹配根节点,并将其重命名为。在那下面你说的是。。因此,这将跳过层次结构并执行所说的操作 您的模板看起来非常完美!!所有你需要的是下面的更正 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:tem

XML



  • 这个“@*| node()”是指二级父节点“foe”吗

    不!这是指子节点和属性。

    如何复制
    添加属性及其父级

    在代码中,您通过说出
    来匹配根节点,并将其重命名为
    。在那下面你说的是
    。。因此,这将跳过层次结构并执行
    所说的操作

    您的
    模板看起来非常完美!!所有你需要的是下面的更正

     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:template match="/">
                <result>
            <xsl:apply-templates select="/root/Algemeen/foto/foe/fee/img"/>
                </result>
            </xsl:template>
    
        <!--specific template match for this img -->
            <xsl:template match="/root/Algemeen/foto/foe/fee/img">
              <xsl:copy>
                    <xsl:attribute name="width">100</xsl:attribute>
                    <xsl:apply-templates select="@*|node()" />
                  </xsl:copy>
            </xsl:template>
    
        <!--Identity template copies content forward -->
            <xsl:template match="@*|node()">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:template>
    
        </xsl:stylesheet>
    
    
    100
    
    如果要访问父级或祖先级,请尝试以下操作:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <result>
          <xsl:apply-templates select="/root/Algemeen/foto"/>
        </result>
      </xsl:template>
    
      <xsl:template match="foto">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <!--specific template match for this img -->
      <xsl:template match="/root/Algemeen/foto/foe/fee/img">
        <xsl:copy>
          <xsl:attribute name="width">100</xsl:attribute>
          <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
      </xsl:template>
    
      <!--Identity template copies content forward -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    
    
    那么,XML文档是什么,您希望得到什么样的结果?请编辑问题并添加此重要的、缺失的信息。谢谢。它是有效的:)。我的最后一种方法与这个方法成反比,我找不到一种方法先得到孩子,然后再得到父母。@iCeR,欢迎!好。不鼓励您提到的反向XPath方法!我提供的是一个更好的方法!我已经提供了从当前节点访问祖先的方法。。但在你的情况下,这不是必需的。。你也不应该这样做。。如果您在理解方面还需要帮助,请告诉我。作为将来的参考:),我听上去很清楚。谢谢你的帮助:)@iCeR,很乐意帮助。。如果您对答案感到满意,请单击我答案左上角的勾号。。因此,它将被接受为此问题的答案,并将完成问答环节!
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <result>
          <xsl:apply-templates select="/root/Algemeen/foto"/>
        </result>
      </xsl:template>
    
      <xsl:template match="foto">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <!--specific template match for this img -->
      <xsl:template match="/root/Algemeen/foto/foe/fee/img">
        <xsl:copy>
          <xsl:attribute name="width">100</xsl:attribute>
          <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
      </xsl:template>
    
      <!--Identity template copies content forward -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    
    <xsl:for-each select="ancestor::foto">