XSLT 1.0, XML to CSV: how to retrieve headers from attributes and child nodes

*爱你&永不变心* 提交于 2019-12-13 07:24:36

问题


Considering the following XML input:

<LIST>
    <DIRECTORIES>
        <DIRECTORY ID="000001">
            <NAME>folder</NAME>
            <LOCATION>C:\here</LOCATION>
        </DIRECTORY>
        <DIRECTORY ID="000002">
            <COLOR>blue</COLOR>
            <LOCATION>C:\here</LOCATION>
            <DATE>01-25-2015</DATE>
        </DIRECTORY>
    <DIRECTORIES>
    <FILES>
        <FILE ID="000001">
            <NAME>file</NAME>
            <TYPE>rt</TYPE>
            <SERVICE>inf</SERVICE>
        </FILE>
        <FILE ID="000002">
            <NAME>f</NAME>
        </FILE>
    </FILES>
<LIST>

I'd like to retrieve the content of DIRECTORIES, with one header per distinct DIRECTORY's child node and attributes. In this example, the desired output would be:

ID, NAME, LOCATION, COLOR, DATE
000001, folder, C:\here,,
000002,,C:\here,blue,01-25-2015

There are a lot of questions regarding XML to CSV transformations, so I tried to tweak a solution offered in another one, without success. Here's my XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:variable name="delimiter" select="','"/>

  <xsl:key name="directoryFields" match="/LIST/DIRECTORIES/DIRECTORY/*" use="local-name()"/>

  <xsl:variable name="dirFields"
       select="/LIST/DIRECTORIES/DIRECTORY/*[generate-id()=generate-id(key('directoryFields', local-name())[1])]" />

  <xsl:template match="/">
    <xsl:for-each select="$dirFields">
      <xsl:value-of select="local-name()" />
      <xsl:if test="position() &lt; last()">
        <xsl:value-of select="$delimiter" />
      </xsl:if>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates select="*/*" />
  </xsl:template>

  <xsl:template match="*">
    <xsl:variable name="this" select="." />
    <xsl:for-each select="$dirFields">
      <xsl:value-of select="$this/*[local-name() = local-name(current())]" />
      <xsl:if test="position() &lt; last()">
        <xsl:value-of select="$delimiter" />
      </xsl:if>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

回答1:


Well:

<xsl:apply-templates select="*/*" />

needs to be:

<xsl:apply-templates select="LIST/DIRECTORIES/DIRECTORY" />

What you have selects LIST/DIRECTORIES. And you cannot use */*/* because that selects the FILE nodes, too.

You also need to remove this line:

<xsl:value-of select="/LIST/DIRECTORIES/DIRECTORY"/>

added

In order to include the ID, you must add this:

<xsl:value-of select="concat(@ID, $delimiter)" />

to the template processing DIRECTORY, before moving on to process the fields.



来源:https://stackoverflow.com/questions/31610551/xslt-1-0-xml-to-csv-how-to-retrieve-headers-from-attributes-and-child-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!