Xml 提取标记名、属性及其值

Xml 提取标记名、属性及其值,xml,xslt,Xml,Xslt,示例xml文件如下所示 <a> <apple color="red"/> <banana color="yellow"/> <sugar taste="sweet"/> <cat size="small"/> </a> 为了获得下面的示例输出,我应该在XSLT中编写什么 <AAA>apple</AAA> <BBB>color</BBB> <CCC>red<

示例xml文件如下所示

<a>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</a>

为了获得下面的示例输出,我应该在XSLT中编写什么

<AAA>apple</AAA>
<BBB>color</BBB>
<CCC>red</CCC>
<AAA>banana</AAA>
<BBB>color</BBB>
<CCC>yellow</CCC>
苹果公司 颜色 红色 香蕉 颜色 黄色的 下面是我编写的XSLT文件,但我不知道如何提取值

<xsl:template match="*/*">
<AAA>
  <xsl:value-of select="name()"/>
</AAA>
  <xsl:apply-templates select="@*"/>
</xsl:template>
<xsl:template match="@*">
<BBB>
  <xsl:value-of select="name()"/>
</BBB>
</xsl:template>


等等。如果你不知道复制(或标识)模板的习惯用法,那么去谷歌搜索它;没有它,您的XSLT生活将是悲惨的。

您的xml应该是

<catalog>
    <fruit>
        <name>apple </name>
        <color>red</color>
    </fruit>
    <fruit>
        <name>banana  </name>
        <color>yellow</color>
    </fruit>
</catalog>

苹果
红色
香蕉
黄色的
XSLT作为:

 <xsl:for-each select="catalog/fruit">
      <tr>
        <td><AAA><xsl:value-of select="title"/></AAA></td>
        <td><BBB>color</BBB></td>
        <td><CCC><xsl:value-of select="color"/></CCC></td>
      </tr>
      </xsl:for-each>

颜色

此转换:

<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="*/*[not(self::sugar or self::cat)]">
  <AAA><xsl:value-of select="name()"/></AAA>
  <BBB><xsl:value-of select="name(@*)"/></BBB>
  <CCC><xsl:value-of select="@*"/></CCC>
 </xsl:template>
</xsl:stylesheet>
<a>
    <apple color="red"/>
    <banana color="yellow"/>
    <sugar taste="sweet"/>
    <cat size="small"/>
</a>
<AAA>apple</AAA>
<BBB>color</BBB>
<CCC>red</CCC>
<AAA>banana</AAA>
<BBB>color</BBB>
<CCC>yellow</CCC>

应用于提供的XML文档时

<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="*/*[not(self::sugar or self::cat)]">
  <AAA><xsl:value-of select="name()"/></AAA>
  <BBB><xsl:value-of select="name(@*)"/></BBB>
  <CCC><xsl:value-of select="@*"/></CCC>
 </xsl:template>
</xsl:stylesheet>
<a>
    <apple color="red"/>
    <banana color="yellow"/>
    <sugar taste="sweet"/>
    <cat size="small"/>
</a>
<AAA>apple</AAA>
<BBB>color</BBB>
<CCC>red</CCC>
<AAA>banana</AAA>
<BBB>color</BBB>
<CCC>yellow</CCC>

生成所需的正确结果

<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="*/*[not(self::sugar or self::cat)]">
  <AAA><xsl:value-of select="name()"/></AAA>
  <BBB><xsl:value-of select="name(@*)"/></BBB>
  <CCC><xsl:value-of select="@*"/></CCC>
 </xsl:template>
</xsl:stylesheet>
<a>
    <apple color="red"/>
    <banana color="yellow"/>
    <sugar taste="sweet"/>
    <cat size="small"/>
</a>
<AAA>apple</AAA>
<BBB>color</BBB>
<CCC>red</CCC>
<AAA>banana</AAA>
<BBB>color</BBB>
<CCC>yellow</CCC>
苹果公司 颜色 红色 香蕉 颜色 黄色的
注意:假设每个匹配的元素只有一个属性,提供的XML文档就是这样。

我的XML文件就是这样给出的,不是一个错误模板应该是自动匹配的,代码不允许使用上面的示例XSLT代码,但我不知道如何提取它的值~