【问题标题】:Grouping across children of sibling elements跨兄弟元素的子元素分组
【发布时间】:2014-07-24 11:50:35
【问题描述】:

我正在使用 XSLT 处理 IDML 文件。在从 InDesign 导出的 IDML 中,它同时运行相同样式的后续段落,由<Br/> 标签分隔,如下所示(我的输入 XML):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Story>
    <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/para">
        <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
            <Content>All rights reserved. No part of this publication may be reproduced in any material form (including photocopying or storing it in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright owner except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the </Content>
        </CharacterStyleRange>
        <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/italic">
            <Content>Copyright Licensing Agency Ltd, Saffron House, 6-10 Kirby Street, London, EC1N 8TS England</Content>
        </CharacterStyleRange>
        <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
            <Content>. Applications for the copyright owner’s written permission to reproduce any part of this publication should be addressed to the publisher.</Content>
            <Br/>
            <Content>Warning: The doing of an unauthorised act in relation to a copyright work may result in both a civil claim for damages and criminal prosecution.</Content>
            <Br/>
            <Content>Crown copyright material is reproduced with the permission of the Controller of HMSO and the Queen’s Printer for Scotland.</Content>
            <Br/>
        </CharacterStyleRange>
    </ParagraphStyleRange>
</Story>

现在,我需要将其转换成如下所示的 XML

<?xml version="1.0" encoding="UTF-8"?>
<story> 
    <para>All rights reserved. No part of this publication may be reproduced in any material form (including photocopying or storing it in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright owner except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the <italic>Copyright Licensing Agency Ltd, Saffron House, 6-10 Kirby Street, London, EC1N 8TS England</italic>. Applications for the copyright owner’s written permission to reproduce any part of this publication should be addressed to the publisher.</para>
    <para>Warning: The doing of an unauthorised act in relation to a copyright work may result in both a civil claim for damages and criminal prosecution.</para>
    <para>Crown copyright material is reproduced with the permission of the Controller of HMSO and the Queen’s Printer for Scotland.</para>
</story>

我的 XSL 如下所示:

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

    <xsl:template match="/Story">
        <story>
            <xsl:apply-templates/>
        </story>
    </xsl:template>

    <xsl:template match="ParagraphStyleRange">
        <xsl:apply-templates>
            <xsl:with-param name="para_style_name" select="replace(./@AppliedParagraphStyle, 'ParagraphStyle/', '')"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="CharacterStyleRange">
        <xsl:param name="para_style_name"/>
        <xsl:variable name="char_style_name" select="replace(./@AppliedCharacterStyle, 'CharacterStyle/', '')"/>
        <xsl:for-each-group select="*" group-ending-with="Br">
            <xsl:element name="{$para_style_name}">
                <xsl:choose>
                    <xsl:when test="$char_style_name = '$ID/[No character style]'">
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:element name="{$char_style_name}">
                            <xsl:apply-templates select="current-group()"/>
                        </xsl:element>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="Content">
        <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match="Br"/>

</xsl:stylesheet>

这几乎可以工作,除了第一个 para 在 italic 部分拆分。

<?xml version="1.0" encoding="UTF-8"?>
<story> 
    <para>All rights reserved. No part of this publication may be reproduced in any material form (including photocopying or storing it in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright owner except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the </para>
    <para><italic>Copyright Licensing Agency Ltd, Saffron House, 6-10 Kirby Street, London, EC1N 8TS England</italic></para>
    <para>. Applications for the copyright owner’s written permission to reproduce any part of this publication should be addressed to the publisher.</para>
    <para>Warning: The doing of an unauthorised act in relation to a copyright work may result in both a civil claim for damages and criminal prosecution.</para>
    <para>Crown copyright material is reproduced with the permission of the Controller of HMSO and the Queen’s Printer for Scotland.</para>
</story>

正如预期的那样,当跨越输入中的多个 CharacterStyleRange 元素时,我的分组方法不起作用。但是有没有一种可以工作的分组方法?还是我最好采用不同的方法,例如在每个 Br 处终止 CharacterStyleRange 和 ParagraphStyleRange 并重新打开它们作为简化处理的中间步骤?

【问题讨论】:

    标签: xslt idml


    【解决方案1】:

    所以,你快到了。将分组上移一级,在匹配 ParagraphStyleRange 的模板中进行:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="Story">
        <story>
            <xsl:apply-templates select="ParagraphStyleRange"/>
        </story>
    </xsl:template>
    
    <xsl:template match="ParagraphStyleRange">
        <xsl:variable name="para_style_name" select="replace(@AppliedParagraphStyle, 'ParagraphStyle/', '')"/>
        <xsl:for-each-group select="CharacterStyleRange/*" group-ending-with="Br">
            <xs:element name="{para_style_name}">
                <xsl:apply-templates select="current-group()[self::Content]"/>
            </xs:element>
        </xsl:for-each-group>
    </xsl:template>
    
    <xsl:template match="Content">
        <xsl:variable name="char_style_name" select="replace(../@AppliedCharacterStyle, 'CharacterStyle/', '')"/>
        <xsl:choose>
            <xsl:when test="$char_style_name = '$ID/[No character style]'">
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{$char_style_name}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-14
      • 2021-07-10
      • 2017-04-07
      • 1970-01-01
      • 2013-03-11
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多