Xml 从不同的结构中为每个应用程序创建XSL
我在实现此XSLT时遇到了一个问题,我需要的是从以下XML创建一个HTML表:Xml 从不同的结构中为每个应用程序创建XSL,xml,xslt,variables,foreach,Xml,Xslt,Variables,Foreach,我在实现此XSLT时遇到了一个问题,我需要的是从以下XML创建一个HTML表: <?xml version="1.0" encoding="ISO-8859-1"?> <Overview> <Header> <Column name="SysRepositoryLabel_Language" width="10%" align="center">Lingua</Column> <Column name="SysRe
<?xml version="1.0" encoding="ISO-8859-1"?>
<Overview>
<Header>
<Column name="SysRepositoryLabel_Language" width="10%" align="center">Lingua</Column>
<Column name="SysRepositoryLabel_Text" width="45%" align="left">Testo</Column>
<Column name="SysRepositoryLabel_Usage" width="45%" align="left">Uso</Column>
</Header>
<Rows>
<Row row="0">
<Column name="SysRepositoryLabel_PKey">1</Column>
<Column name="SysRepositoryLabel_Language">it</Column>
<Column name="SysRepositoryLabel_Text">Accedi al Sistema</Column>
<Column name="SysRepositoryLabel_Usage">AppMenuLabelLogIn</Column>
</Row>
<Row row="1">
<Column name="SysRepositoryLabel_PKey">2</Column>
<Column name="SysRepositoryLabel_Language">en</Column>
<Column name="SysRepositoryLabel_Text">LogIn</Column>
<Column name="SysRepositoryLabel_Usage">AppMenuLabelLogIn</Column>
</Row>
<Row row="2">
<Column name="SysRepositoryLabel_PKey">3</Column>
<Column name="SysRepositoryLabel_Language">it</Column>
<Column name="SysRepositoryLabel_Text">Archivio</Column>
<Column name="SysRepositoryLabel_Usage">AppMenuLabelMasterData</Column>
</Row>
</Rows>
</Overview>
语言
特斯托
Uso
1.
信息技术
阿克迪·西斯特马酒店
阿普门努拉贝洛金
2.
EN
登录
阿普门努拉贝洛金
3.
信息技术
阿克维奥
AppMenuLabelMasterData
这是我想要的表格:
<table width="100%" cellpadding="0" cellspacing="0" border="0>
<tr>
<td width="10%" align="center" valign="center">Lingua</td>
<td width="45%" align="left" valign="center">Testo</td>
<td width="45%" align="left" valign="center">Uso</td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="10%" align="center" valign="center">it</td>
<td width="45%" align="left" valign="center">Accedi al Sistema</td>
<td width="45%" align="left" valign="center">AppMenuLabelLogIn</td>
</tr>
<tr>
<td width="10%" align="center" valign="center">en</td>
<td width="45%" align="left" valign="center">LogIn</td>
<td width="45%" align="left" valign="center">AppMenuLabelLogIn</td>
</tr>
<tr>
<td width="10%" align="center" valign="center">it</td>
<td width="45%" align="left" valign="center">Archivio</td>
<td width="45%" align="left" valign="center">AppMenuLabelMasterData</td>
</tr>
</table>
以下是一个示例,假设您需要一个表,其中的thead由Header/Column元素填充,tbody由Rows/Row元素填充。我使用模板,而不是每个模板:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="html" indent="yes"/>
<xsl:variable name="cols" select="/Overview/Header/Column"/>
<xsl:template match="Overview">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<xsl:apply-templates select="$cols" mode="cols"/>
<thead>
<tr>
<xsl:apply-templates select="$cols"/>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="Rows/Row"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="Header/Column" mode="cols">
<col width="{@width}" align="{@align}" valign="center"/>
</xsl:template>
<xsl:template match="Header/Column">
<th>
<xsl:value-of select="."/>
</th>
</xsl:template>
<xsl:template match="Row">
<tr>
<xsl:apply-templates select="Column[@name = $cols/@name]"/>
</tr>
</xsl:template>
<xsl:template match="Row/Column">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
Saxon 6.5.5,当针对发布的输入运行样式表时,输出
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<col width="10%" align="center" valign="center">
<col width="45%" align="left" valign="center">
<col width="45%" align="left" valign="center">
<thead>
<tr>
<th>Lingua</th>
<th>Testo</th>
<th>Uso</th>
</tr>
</thead>
<tbody>
<tr>
<td>it</td>
<td>Accedi al Sistema</td>
<td>AppMenuLabelLogIn</td>
</tr>
<tr>
<td>en</td>
<td>LogIn</td>
<td>AppMenuLabelLogIn</td>
</tr>
<tr>
<td>it</td>
<td>Archivio</td>
<td>AppMenuLabelMasterData</td>
</tr>
</tbody>
</table>
语言
特斯托
Uso
信息技术
阿克迪·西斯特马酒店
阿普门努拉贝洛金
EN
登录
阿普门努拉贝洛金
信息技术
阿克维奥
AppMenuLabelMasterData
这就是我今晚找到的解决方案;)
...
我没有发布所有的代码,因为现在我添加了很多功能,这可能会让我们失去问题的焦点。但是有了这段代码,我能够用节点标题的相同列过滤节点行的列,同时维护标题节点的属性(width,align).您可以写“这是我想要获得的表”,但随后您发布了两个HTML表的标记,而不是一个HTML表的标记。请澄清您正在寻找的输出。您好,非常感谢您的支持,但不幸的是,这种解决方案对我不好,出于技术原因,我需要使用a for each应用它,非常感谢anyway@user1477028:这根本不是“技术”原因。这是一个表明您需要学习XSLT的问题的个人原因。SO的主要目的是向专家学习,而你似乎并不想学习+1对于Martin Honnen的好答案,大家好,我确实有很多关于XSLT的东西要学习,请记住我昨天第一次开始学习XSLT。问题在于,使用JQuery应用工作表时没有考虑:(仅此而已。由于我目前还不是专家,我认为作为模板的模板和简单的XSLT之间有很大的区别,我等待8小时后发布我的解决方案,因为我认为问题很容易解决,但我还不太了解XSL指针。非常感谢。)顺便说一句……您好,这是您的代码在我的代码中不起作用的部分,“Column[@name=$cols/@name]”,似乎属性$cols/@name抛出了一个错误,所以我的转换结果是一个空白页:(我将它放在一个for-each中,如下所示:
<xsl:variable name="Cols" select="Overview/Header/Column"/>
...
<xsl:for-each select="Column[@name = $Cols//@name]">
<td valign="center">
<xsl:variable name="CurrentCol" select="@name"/>
<xsl:for-each select="$Cols[@name = $CurrentCol]">
<xsl:attribute name="width"><xsl:value-of select="@width" /></xsl:attribute>
<xsl:attribute name="align"><xsl:value-of select="@align" /></xsl:attribute>
</xsl:for-each>
<xsl:value-of select="."/>
</td>
</xsl:for-each>