XSLT将HTML列表转换为XML

XSLT将HTML列表转换为XML,xml,xslt,xhtml,Xml,Xslt,Xhtml,问题: 解决了这个问题,我能够生成一个脚本,它将遍历我的XHTML列表并生成XML输出 问题是: 当我有一个有序列表时,XSLT应该是什么样子 谢谢@Sbof 我需要生成以下XML: <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/ul"> <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Character Style 1">

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

abc

xyz
abc
拉丁字母
xyz
abc
我有一个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>

  • abc
  • xyz
  • abc
  • xyzabc
  • xyz
  • abc
  • xyzxyz
  • abc
  • xyz
  • 下一级
  • 这是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>
    
    
    第%3条1级OL
    
    这是我使用脚本得到的结果:

    <?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>
    
    
    abc
    
    xyz
    abc
    拉丁字母
    xyz
    abc
    xyzxyz
    abc
    xyz
    下一级
    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>
    
    
    

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

    以下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>
    
    
    段落风格/ol级别
    人物风格
    
    我使用的确切输入是

    <!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>
    
    
    
  • 第1A级
  • 1b级
  • 级别2A
  • 2b级
  • 级别2C
  • 级别3A

  • 您可能需要根据您的具体需要对其进行调整。

    它是否应该看起来像您的代码片段?是的,您在这方面的发言权,我非常接近。你知道有什么好主意或提示可以从这里走到哪里吗?这段代码深入并得到了所有信息,我想把它按级别分解。我正在转换XHTML以生成XML,但生成的XML会创建另一个结果。我也会分享这一点。如果你能在问题中准确地解释你的问题所在,那将非常有帮助。对不起,我会再试一次。我需要生成一个脚本,该脚本将遍历一个列表并创建一个属性“AppliedParagraphStyle”,其值为当前的级别。在递归中,我们从级别1开始,然后我们有一个子列表,所以我们现在处于级别2,等等。我希望这会有所帮助。但是,出于某种原因,我的脚本会深入并将所有级别作为级别1输出。