xml - XSLT 扁平化 XML

标签 xml xslt

有人可以帮我进行以下转换吗?

这里是输入的xml:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author>
        <name>Smith</name>
    </author>
    <author>
        <name>Wallace</name>
    </author>
    <author>
        <name>Brown</name>
    </author>
</book>
<book>
    <title>Other book</title>
    <pages>100</pages>
    <size>small</size>
    <author>King</author>
</book>
<book>
    <title>Pretty book</title>
    <pages>150</pages>
    <size>medium</size>
</book>

这是期望的输出

<book style="even">
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author-name>Smith</author-name>
</book>
<book style="odd">
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author-name>Wallace</author-name>
</book>
<book style="even">
    <title>My book</title>
    <pages>200</pages>
    <size>big</size>
    <author-name>Brown</author-name>
</book>
<book style="odd" >
    <title>Other book</title>
    <pages>100</pages>
    <size>small</size>
    <author-name>King</author-name>
</book>
<book style="even">
    <title>Pretty book</title>
    <pages>150</pages>
    <size>medium</size>
    <author-name />
</book>

我尝试使用 xsl:for-each 循环,但我想它们把我带到了死胡同。这里棘手的部分是“style”属性,无论在任何书中放置了多少作者标签,​​它都需要以某种方式“全局”。

最佳答案

这个简单的纯 XSLT 1.0 转换(没有条件,没有 xsl:for-each,没有参数传递,没有 xsl:element,没有使用臭名昭著的低效 //):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my" >
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <my:names>
     <n>odd</n>
     <n>even</n>
    </my:names>

    <xsl:variable name="vStyles"
        select="document('')/*/my:names/*"/>

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

    <xsl:template match="book/author">
     <xsl:variable name="vPos">
      <xsl:number level="any" count="book/author|book[not(author)]"/>
     </xsl:variable>
        <book style="{$vStyles[$vPos mod 2 +1]}">
          <xsl:copy-of select="@*|../node()[not(self::author)]"/>
          <author-name>
            <xsl:value-of select="normalize-space()"/>
      </author-name>
        </book>
    </xsl:template>

    <xsl:template match="book[author]">
     <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="book[not(author)]">
     <xsl:variable name="vPos">
      <xsl:number level="any" count="book/author|book[not(author)]"/>
     </xsl:variable>
        <book style="{$vStyles[$vPos mod 2 +1]}">
          <xsl:copy-of select="@*|node()"/>
          <author-name/>
        </book>
    </xsl:template>

    <xsl:template match="book[author]/*[not(self::author)]"/>
</xsl:stylesheet>

应用于此 XML 文档时(提供的包装在单个顶部元素中):

<t>
    <book>
        <title>My book</title>
        <pages>200</pages>
        <size>big</size>
        <author>
            <name>Smith</name>
        </author>
        <author>
            <name>Wallace</name>
        </author>
        <author>
            <name>Brown</name>
        </author>
    </book>
    <book>
        <title>Other book</title>
        <pages>100</pages>
        <size>small</size>
        <author>King</author>
    </book>
    <book>
        <title>Pretty book</title>
        <pages>150</pages>
        <size>medium</size>
    </book>
</t>

产生完全想要的、正确的结果:

<t>
   <book style="even">
      <title>My book</title>
      <pages>200</pages>
      <size>big</size>
      <author-name>Smith</author-name>
   </book>
   <book style="odd">
      <title>My book</title>
      <pages>200</pages>
      <size>big</size>
      <author-name>Wallace</author-name>
   </book>
   <book style="even">
      <title>My book</title>
      <pages>200</pages>
      <size>big</size>
      <author-name>Brown</author-name>
   </book>
   <book style="odd">
      <title>Other book</title>
      <pages>100</pages>
      <size>small</size>
      <author-name>King</author-name>
   </book>
   <book style="even">
      <title>Pretty book</title>
      <pages>150</pages>
      <size>medium</size>
      <author-name/>
   </book>
</t>

说明:适当使用 xsl:number 和模板/模式匹配。

关于xml - XSLT 扁平化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7674014/

相关文章:

xslt - 在XSL中获取父节点属性

java - 将 xml 转换为 csv

android - Android 应用程序中的日历以选择月份和年份

php - 无法打开流 : Permission denied file_get_contents which generates XML FILE

android - 如何在 ConstraintLayout 中设置 View 的绝对位置

sql-server - SQL Server 将 XML 子节点 append 到父节点

c# - 如何使用 C# 代码检测 XSLT 转换中的 "parsing time"?

xml - XSLT将线性结构转换为非线性

C# XSLT 快速转换大型 XML 文件

php - 使用 DOMDocument 在 XML 中附加 QBXML 声明