我目前正在尝试使用XSL转换以下XML结构
<table name="PERSONS" schema="INO_PERSONS_MGMT" syntheticPK="ID">
<columns>
<column type="NUMBER" primary="true" unique="true">key</column>
<column type="varchar">name</column>
<column type="varchar">comment</column>
<column type="varchar">email</column>
</columns>
<records>
<record key="1" name="Test1" email="test1@test.com"/>
<record key="2" name="Test2" comment="something" mail="test2@test.com"/>
</records>
到以下xml输出:
<table:table table:name="INO_PERSONS_MGMT.PERSONS">
<table:table-row>
<table:table-cell>
<text:p>key</text:p>
</table:table-cell>
<table:table-cell>
<text:p>name</text:p>
</table:table-cell>
<table:table-cell>
<text:p>comment</text:p>
</table:table-cell>
<table:table-cell>
<text:p>email</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell>
<text:p>NUMBER</text:p>
</table:table-cell>
<table:table-cell>
<text:p>varchar</text:p>
</table:table-cell>
<table:table-cell>
<text:p>varchar</text:p>
</table:table-cell>
<table:table-cell>
<text:p>varchar</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell>
<text:p>1</text:p>
</table:table-cell>
<table:table-cell>
<text:p>Test1</text:p>
</table:table-cell>
<table:table-cell>
<text:p></text:p>
</table:table-cell>
<table:table-cell>
<text:p>test1@test.com</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell>
<text:p>2</text:p>
</table:table-cell>
<table:table-cell>
<text:p>Test2</text:p>
</table:table-cell>
<table:table-cell>
<text:p>something</text:p>
</table:table-cell>
<table:table-cell>
<text:p>test2@test.com</text:p>
</table:table-cell>
</table:table-row>
</table:table>
因此,数据表行(第2行和第3行)的结构应该由列的结构定义。因此,它应迭代给定的列并生成表格单元格,即使它们未在记录中定义。
答案 0 :(得分:2)
您显示的输出不是格式良好的XML,因为table:
前缀未绑定到命名空间。
要最小化实际问题的示例,请考虑以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/table">
<xsl:variable name="cols" select="columns/column" />
<table name="{@name}">
<!-- header rows -->
<row>
<xsl:for-each select="$cols">
<cell>
<xsl:value-of select="."/>
</cell>
</xsl:for-each>
</row>
<row>
<xsl:for-each select="$cols">
<cell>
<xsl:value-of select="@type"/>
</cell>
</xsl:for-each>
</row>
<!-- data rows -->
<xsl:for-each select="records/record">
<xsl:variable name="curr-row" select="." />
<row>
<xsl:for-each select="$cols">
<cell>
<xsl:value-of select="$curr-row/@*[name()=current()]"/>
</cell>
</xsl:for-each>
</row>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<table name="PERSONS">
<row>
<cell>key</cell>
<cell>name</cell>
<cell>comment</cell>
<cell>email</cell>
</row>
<row>
<cell>NUMBER</cell>
<cell>varchar</cell>
<cell>varchar</cell>
<cell>varchar</cell>
</row>
<row>
<cell>1</cell>
<cell>Test1</cell>
<cell/>
<cell>test1@test.com</cell>
</row>
<row>
<cell>2</cell>
<cell>Test2</cell>
<cell>something</cell>
<cell/>
</row>
</table>
这应该很容易让你适应你的情况。
请注意,"email"
与"mail"
不同 - 这就是第二行的最后一个单元格为空的原因。