如果没有正确的属性,XSL会重复一个值

时间:2019-03-05 01:00:33

标签: xml xslt

我有一个XSL样式表,可以转换一个平面XML文件(该文件必须是平面文件-这是软件输出文件的方式)。 看起来像这样:

<Content>
    <Paragraph Number="1" Type="Scene Heading">
        <Text>Scene Heading</Text>
    </Paragraph>
    <Paragraph Type="Action">
        <Text>Action Text</Text>
    </Paragraph>
    <Paragraph Type="Character">
        <Text>Character</Text>
    </Paragraph>
    <Paragraph Type="Dialogue">
        <Text>Dialogue Text</Text>
    </Paragraph>
    <Paragraph Number="2" Type="Scene Heading">
        <Text>Scene Heading</Text>
    </Paragraph>
    <Paragraph Type="Action">
        <Text>Action Text</Text>
    </Paragraph>
    <Paragraph Type="Character">
        <Text>Character</Text>
    </Paragraph>
    <Paragraph Type="Dialogue">
        <Text>Dialogue Text</Text>
    </Paragraph>
    <Paragraph Type="Character">
        <Text>Character</Text>
    </Paragraph>
    <Paragraph Type="Dialogue">
        <Text>Dialogue Text</Text>
    </Paragraph>
    .
    .
    .
</Content>

我想要一张这样的桌子:

Scene#    Type        Text
1         Scene Head. Scene Heading Text
1         Action      Action Text
1         Character   Character Text
1         Dialogue    Dialogue Text
2         Scene Head. Scene Heading Text
2         Action      Action Text
2         Character   Character Text
2         Dialogue    Dialogue Text
2         Character   Character Text
2         Dialogue    Dialogue Text

但是我只能这样做,即场景编号位于场景标题处,如下所示:

Scene#    Type        Text
1         Scene Head. Scene Heading Text
          Action      Action Text
          Character   Character Text
          Dialogue    Dialogue Text
2         Scene Head. Scene Heading Text
          Action      Action Text
          Character   Character Text
          Dialogue    Dialogue Text
          Character   Character Text
          Dialogue    Dialogue Text

有没有办法做到这一点?

我的XSL现在看起来像这样:

<ROW>
                        <!-- Type
            -->
            <COL>
                <DATA>
                    <xsl:value-of select="@Type"/></DATA>
            </COL>
                        <!-- Scene Number
            -->
            <COL>
                <DATA>
                    <xsl:if test="@Type='Scene Heading'">
                    <xsl:value-of select="@Number"/></xsl:if></DATA>
            </COL>

            <!-- Type
            -->
             <COL>
                <DATA>
                    <xsl:value-of select="Text"/></DATA>
            </COL>
</ROW>

谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

如果上下文节点为Paragraph,则可以使用以下表达式检索当前或最后一个“场景标题” Number属性:

<xsl:value-of select="(self::Paragraph[@Type='Scene Heading'] | preceding-sibling::Paragraph[@Type='Scene Heading'][1])[last()]/@Number"/>

将结果放入您的模板中。

答案 1 :(得分:0)

假设您位于<xsl:for-each select="Paragraph">区块内,则可以执行以下操作:

<xsl:choose>
    <xsl:when test="@Number">
        <xsl:value-of select="@Number"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="preceding-sibling::Paragraph[@Number][1]/@Number"/>
    </xsl:otherwise>
</xsl:choose>

但是,这种不断地来回移动的效率很低。我建议使用另一种方法:

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:strip-space elements="*"/>

<xsl:key name="sub-para" match="Paragraph[not(@Number)]" use="generate-id(preceding-sibling::Paragraph[@Number][1])" />

<xsl:template match="/Content">
    <!-- OTHER STUFF -->
    <RESULTSET>
        <xsl:for-each select="Paragraph[@Number]">
            <xsl:variable name="scene-number" select="@Number" />
            <xsl:for-each select=". | key ('sub-para', generate-id())">
                <ROW>
                    <COL>
                        <DATA>
                            <xsl:value-of select="$scene-number"/>
                        </DATA>
                    </COL>
                    <!-- OTHER COLUMNS -->
                </ROW>
            </xsl:for-each>       
        </xsl:for-each>
    </RESULTSET>
    <!-- OTHER STUFF -->
</xsl:template>

</xsl:stylesheet>