0

问题: 已解决,我能够生成一个脚本,该脚本将通过我的 XHTML 列表并生成 XML 输出。

问题是:当我有一个有序列表时,XSLT 应该是什么样子?

谢谢@Sbof

我需要生成以下 XML:

<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ul">
  <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
    <Content>abc</Content>
    <Br/>
    <Content>xyz</Content>
    <Br/>
    <Content>abc</Content>
    <Br/>
    <Content>xyzabc</Content>
    <Br/>
  </CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 1">
  <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
    <Content>xyz</Content>
    <Br/>
  </CharacterStyleRange>
</ParagraphStyleRange>
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 2">
  <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
    <Content>abc</Content>
    <Br/>
  </CharacterStyleRange>
</ParagraphStyleRange>

我有看起来像这样的 XHTML(内容不同,但做同样的事情):

<?xml version="1.0" encoding="utf-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
<ol>
    <li>abc</li>
    <li>xyz</li>
    <li>abc</li>
    <li>xyzabc</li>
    <li>xyz<ol>
        <li>abc</li>
        </ol>
    </li>
    <li>xyzxyz</li>
    <li>abc</li>
    <ol>
        <li>xyz</li>
    </ol>
    <li>next level</li>
</ol>
</body>
</html>

这是横穿 XHTML 的 XSLT 片段:

  <xsl:template match="xhtml:ol/xhtml:li[not(*)]">
    <xsl:call-template name="para-style-range">
      <xsl:with-param name="style-name">article%3aol Level 1</xsl:with-param>
    </xsl:call-template>
    <xsl:if test ="xhtml:ol/xhtml:li[*]|
                   xhtml:ul/xhtml:li[*]">
      <xsl:apply-templates select="xhtml:ol/xhtml:li[*]|
                                   xhtml:ul/xhtml:li[*]" />
    </xsl:if>
  </xsl:template>

这是我使用脚本得到的结果:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="7.5">
    <Story Self="ucb" AppliedTOCStyle="n" TrackChanges="false" StoryTitle="$ID/" AppliedNamedGrid="n">
        <StoryPreference OpticalMarginAlignment="false" OpticalMarginSize="12" FrameType="TextFrameType" StoryOrientation="Horizontal" StoryDirection="LeftToRightDirection"/>
        <InCopyExportOption IncludeGraphicProxies="true" IncludeAllResources="false"/>
        <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ul">
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
                <Content>abc</Content>
                <Br/>
                <Content>xyz</Content>
                <Br/>
                <Content>abc</Content>
                <Br/>
                <Content>xyzabc</Content>
                <Br/>
            </CharacterStyleRange>
        </ParagraphStyleRange>
        <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 1">
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
                <Content>xyz</Content>
                <Br/>
            </CharacterStyleRange>
        </ParagraphStyleRange>
        <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 2">
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
                <Content>abc</Content>
                <Br/>
            </CharacterStyleRange>
        </ParagraphStyleRange>
        <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 1">
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
                <Content>xyzxyz</Content>
                <Br/>
                <Content>abc</Content>
                <Br/>
            </CharacterStyleRange>
        </ParagraphStyleRange>
        <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 3">
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
                <Content>xyz</Content>
                <Br/>
            </CharacterStyleRange>
        </ParagraphStyleRange>
        <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ol level 1">
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">
                <Content>next level</Content>
                <Br/>
            </CharacterStyleRange>
        </ParagraphStyleRange>
    </Story>
</idPkg:Story>

XSLT 截取段落的属性值:

<xsl:template name="para-style-range">
    <!-- The name of the paragraph style in InDesign -->
    <xsl:param name="style-name"/>
    <xsl:param name ="isTable" />
    <!-- A string of text that will precede the paragraph's actual content (ex: 'by ')-->
    <xsl:param name="prefix-content" select="''"/>
    <ParagraphStyleRange>
      <xsl:attribute name="AppliedParagraphStyle">
        <xsl:value-of select="concat('ParagraphStyle/', $style-name)"/>
      </xsl:attribute>
      <xsl:if test="$prefix-content != ''">
        <CharacterStyleRange>
          <Content><xsl:value-of select="$prefix-content"/></Content>
        </CharacterStyleRange>
      </xsl:if>
      <xsl:apply-templates select="text()|*" mode="character-style-range"/>
      <xsl:choose>
        <xsl:when test ="$isTable = 'true'">
          <!--Dont do any thing here-->
        </xsl:when>
        <xsl:otherwise>
          <Br/>
        </xsl:otherwise>
      </xsl:choose>

    </ParagraphStyleRange>
  </xsl:template>

让我知道我是否可以提供更多脚本来帮助那些仍然对代码感到困惑或需要帮助弄清楚它的人。

4

1 回答 1

0

以下 XSL 适用于您的示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:html="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="html">
    <xsl:template match="text()"/>
    <xsl:template match="/">
        <rootElement>
            <xsl:apply-templates/>
        </rootElement>
    </xsl:template>
    <xsl:template match="//html:ol">
        <ParagraphStyleRange>
            <xsl:attribute name="AppliedParagraphStyle">
                <xsl:text>ParagraphStyle/ol level </xsl:text>
                <xsl:value-of select="count(ancestor::html:ol)"/>
            </xsl:attribute>
            <xsl:apply-templates select="child::html:li"/>
        </ParagraphStyleRange>
        <xsl:apply-templates select="descendant::html:ol"/>
    </xsl:template>

    <xsl:template match="//html:li">
        <CharacterStyleRange>
            <xsl:attribute name="AppliedCharacterStyle">
                <xsl:text>CharacterStyle/Character Style </xsl:text>
                <xsl:value-of select="count(ancestor::html:li)"/>
            </xsl:attribute>
        <Content>
            <xsl:value-of select="normalize-space(text())"/>
        </Content>
        <br/>
        </CharacterStyleRange>
    </xsl:template>
</xsl:stylesheet>

我使用的确切输入是

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <ol>
            <li>level1a</li>
            <li>level1b
                <ol>
                    <li>level2a</li>
                    <li>level2b</li>
                    <li>level2c
                        <ol>
                            <li>level3a</li>
                        </ol>
                    </li>
                </ol>
            </li>
        </ol>
    </body>
</html>

您可能需要根据您的确切需求对其进行调整。

于 2012-09-28T10:52:11.110 回答