Xml 简单的XPath映射和转换(非常基本)

Xml 简单的XPath映射和转换(非常基本),xml,xslt,xpath,Xml,Xslt,Xpath,问题:我在两个不同的地方有属性。我只想在输出中使用这些位置中的一个。因此,我必须将(?)对应的属性/ID映射到ElementAttributeID <xsl:template match="/"> <html> <body> <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute"> <xsl

问题:我在两个不同的地方有属性。我只想在输出中使用这些位置中的一个。因此,我必须将(?)对应的属性/ID映射到ElementAttributeID

    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>
这就是我试图转换的XML:

<ID>Testdata</ID>
<Attributes>
    <Attribute>
        <ID>Time</ID>
        <Name>Time</Name>
    </Attribute>
    <Attribute>
        <ID>Place</ID>
        <Name>Place</Name>
    </Attribute>
    <Attribute>
        <ID>Sense</ID>
        <Name>Sense</Name>
    </Attribute>
</Attributes>
<Elements>
    <Element>
        <ID>First</ID>
        <Name>First</Name>
        <MGs>
            <MG>
                <Attributes>
                    <Attribute>
                        <ElementAttributeID>Time</ElementAttributeID>
                    </Attribute>
                    <Attribute>
                        <ElementAttributeID>Place</ElementAttributeID>
                    </Attribute>
                </Attributes>
            </MG>
        </MGs>
    </Element>
</Elements>
    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>
由于所需的输出是:

Time Matches Time 
Place Matches Place
    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>
我需要为每个嵌套一个吗

这是不对的

    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>
您正在匹配模板,然后一次循环每个匹配1个!移除

<xsl:for-each select="." />
    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

在这个上下文中,当您选择模板匹配的元素时,它基本上不起任何作用,因此简单地说,您是在循环您的上下文节点

    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>
另外,请更新您的问题与您的预期输出,我相信我可以提供更详细的帮助

    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

另外,请确保您的示例xslt与示例xml相匹配

在稍微阅读了一下xsl中的测试是如何工作的之后,我就正确了。
    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>
无需进行映射,仅在foreach循环中使用1个变量:

    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

    <xsl:template match="/">
      <html>
        <body>
        <xsl:for-each select="/Elements/Element/MGs/MG/Attributes/Attribute">
          <xsl:variable name="id" select="ElementAttributeID" />
          <div>
            <xsl:value-of select="concat(/Attributes/Attribute[ID=$id]/Name,'matches',/Elements/Element/MGs/MG/Attributes/Attribute/ElementAttributeID)"/>
          </div>
        </xsl:for-each>
        </body>
      </html>
    </xsl:template>
</xsl:stylesheet>