必须有一个通用的方式要改变一些hierachical XML如:

<element1 A="AValue" B="BValue">
   <element2 C="DValue" D="CValue">
      <element3 E="EValue1" F="FValue1"/>
      <element3 E="EValue2" F="FValue2"/>
   </element2>
   ...
</element1>

进展平XML(html)捡选定的属性的沿途提供不同的标签的特性,成为列标题。

<table>
   <tr>
     <th>A_Label</th>
     <th>D_Label</th>
     <th>E_Label</th>
     <th>F_Label</th>
   </tr>
   <tr>
     <td>AValue</td>
     <td>DValue</td>
     <td>EValue1</td>
     <td>FValue1</td>
   </tr>
   <tr>
     <td>AValue</td>
     <td>DValue</td>
     <td>EValue2</td>
     <td>FValue2</td>
   </tr>
<table>

好的,所以没有通用的解决方案由于属性重新贴标签但是你得到什么我的意思是希望。我刚开始在所有XSLT/XPATH东西,所以我将它的工作在很好的时间,但任何线索,将是有益的。

有帮助吗?

解决方案

我不是100%肯定的是你想做的,但这种解决方案可能的工作如果你芯1,element2和element3嵌套一致。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <table>
            <xsl:apply-templates select="//element3"></xsl:apply-templates>
        </table>
    </xsl:template>

    <xsl:template match="element3">
        <tr>
            <td><xsl:value-of select="../../@A"/></td>
            <td><xsl:value-of select="../../@B"/></td>
            <td><xsl:value-of select="../@C"/></td>
            <td><xsl:value-of select="../@D"/></td>
            <td><xsl:value-of select="@E"/></td>
            <td><xsl:value-of select="@F"/></td>
        </tr>
        <xsl:apply-templates select="*"></xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>

其他提示

我需要一个类似的XSLT但与未知的深度,在这里是我是如何做的。

第一,添加一个包装所得HTML表/清单和电话的模板模式="呕吐"平我们XML树:

<xsl:element name="div">
    <xsl:attribute name="class" select="puke" />
    <xsl:apply-templates select="$notice" mode="puke" />
</xsl:element>  

在这里,我们比赛中每个节点可以显示其内容(例如文本())及其属性。我们这样做。我用dl/dt/dd因为我源树是一个复杂的树不能拼合为一个。

<!-- @description: 
    Display all field from the notice so the customer can tell what he want
-->
<xsl:template match="node()" mode="puke">
<xsl:message>
    puking : <xsl:value-of select="local-name( . )" />
</xsl:message>
    <xsl:element name="dl">
        <xsl:element name="dt">
            <xsl:attribute name="class">tagName</xsl:attribute> 
            <xsl:value-of select="local-name( . )" />
        </xsl:element>
        <xsl:element name="dd">
            <xsl:attribute name="class">tagText</xsl:attribute>
            <xsl:value-of select="text()" /></xsl:element> 
        <xsl:element name="dl">
            <xsl:attribute name="class">attr</xsl:attribute>
            <!-- display attribute -->
            <xsl:apply-templates select="@*" />
        </xsl:element>
    </xsl:element>
    <!-- recursive call on node() -->
    <xsl:apply-templates select="./*" mode="puke" />    
</xsl:template>

匹配的属性赋予的节点,并显示它们。

CSS使用的格式得到HTML:

<style>
.puke {
    background-color: #BDD6DE;
    clear: both;
}
.tagName, .attrName {
    float: left;
}
.tagText, .attrText {
    clear: right;
}
</style>

我们已经有一个临*C节读来自Oracle数据库,它呼吁perl脚本,这又执行一些Java提取数据以XML格式从上述数据库,用于调用的一批文件来执行一些vbscript FTPing的文件,其他一些服务器。我真的希望的东西在Fortran。

原来的问题需要澄清:

  • 发生了什么BValue和CValue在原有问题吗?还有一个原因为什么他们不应该的一部分平的结构?
  • 做所有的元素在XML医生有2个属性,或者是这完全是任意的?
  • 只有3种类型的要素和他们总是嵌套所示为例子吗?
  • 可你的芯1重复自己或者这是根元的你的医生?

在XSLT它可以编写一般性的变压器,但是往往更容易编写的样式表中的变换的文件时可采用任何已知的限制入帐户。

我已经用了一个扩展版本的模板下面铺平结构化的XML。警告:有一些情况下特定代码的原始版本(这实际上变XML入CSV),我只是剥夺了我没有测试这个版本。

基本的工作方式应该是清楚的:它打印的一切,不会有节点的儿童和无递归话的模板上的节点()是否有儿童。我不认为它所处理的特性和注释正确,因为它是现在,但是,这不应该很难解决。

<?xml version="1.0" encoding="UTF-8"?>

<!-- XSL template to flatten structured XML, before converting to CSV. -->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

    <xsl:strip-space elements="*" /> 

    <xsl:template match="/">
        <xsl:apply-templates select="//yourElementsToFlatten"/>
    </xsl:template>

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

    <xsl:template match="@*|node()">
        <xsl:choose>
            <!-- If the element has multiple childs, call this template 
                on its children to flatten it-->
            <xsl:when test="count(child::*) > 0">
                <xsl:apply-templates select="@*|node()"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:value-of select="text()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top