0

我正在尝试展平以下 XML:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://pretend.namespace">
    <record>
        <first>1</first>
        <second>2</second>
        <tricky>
            <taxing>3</taxing>
            <mayhem>
                <ohno>4</ohno>
                <boom>5</boom>
            </mayhem>
        </tricky>
    </record>
    <record>
        <first>1</first>
        <second>2</second>
        <tricky>
            <taxing>3</taxing>
            <mayhem>
                <ohno>4</ohno>
                <boom>5</boom>
            </mayhem>
        </tricky>
    </record>
    <record>
        <first>1</first>
        <second>2</second>
        <tricky>
            <taxing>3</taxing>
            <mayhem>
                <ohno>4</ohno>
                <boom>5</boom>
            </mayhem>
        </tricky>
    </record>
</root>

每条记录一行,丢弃记录中的复杂结构。

1,2,3,4,5
1,2,3,4,5
1,2,3,4,5

使用这个 XSLT

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:p="http://pretend.namespace">

<xsl:strip-space elements="*"/> <!-- remove unwanted whitespace? -->

 <!-- Match every p:record -->
<xsl:template match="p:record">
    <xsl:apply-templates/> <!-- now we're inside a <record> do what you can with the contents then give a line return -->
    <xsl:text>&#xA;</xsl:text><!-- this is a line return -->
</xsl:template>

<xsl:template match="p:record//*">
    <xsl:value-of select="."></xsl:value-of><xsl:text>,</xsl:text>
</xsl:template>

<xsl:template match="text()"/> <!-- WORKS: prevents "default output" to aid debugging -->

</xsl:stylesheet>

但是尽管尝试了几个小时,我还是无法让它访问每个后代并用逗号分隔它们,我得到了这个:

1,2,345,
1,2,345,
1,2,345,

我需要做什么才能让它单独对待所有孙子及以下?(每行的最后一个逗号不是问题)

谢谢!

编辑:this question之后的讨论表明Herong Yang博士的Notepad ++ XML工具似乎只支持XSLT 1.0

4

2 回答 2

1

为什么不简单

<xsl:strip-space elements="*"/>
<xsl:template match="record">
  <xsl:value-of select="descendant::text()/data()" separator=","/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

(需要显式原子化 using data(),否则相邻的文本节点会在没有分隔符的情况下连接起来)。

于 2017-08-13T22:03:39.833 回答
0

怎么样:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://pretend.namespace">
<xsl:output method="text" encoding="utf-8" />
<xsl:strip-space elements="*"/>

<xsl:template match="p:record">
    <xsl:apply-templates/>
    <xsl:text>&#xA;</xsl:text>
</xsl:template>

<xsl:template match="*[not(*)]">
    <xsl:value-of select="."/>
    <xsl:if test="position()!=last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
于 2017-08-13T20:51:52.623 回答