XSLT将HTML列表转换为XML

时间:2012-09-27 19:53:45

标签: xml xslt xhtml

问题: 解决了,我能够生成一个脚本,它将通过我的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>

如果我能提供更多脚本来帮助那些仍然对代码感到困惑或需要帮助解决问题的人,请告诉我。

1 个答案:

答案 0 :(得分: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>

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