2012-09-27 388 views
0

问题: 解决,我是能够产生一个脚本,将通过我的XHTML列表,并产生XML输出。XSLT转换HTML列表转换成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> 

让我知道如果我可以提供更多的脚本来帮助那些谁仍然对代码感到困惑,或者需要帮助解决它。

+0

它不应该看起来像你的片段? – Marcin

+0

是的,你的权利,我相当接近。你知道有什么好主意或暗示从哪里出发?这段代码深入并获得了一切,我想按级别分解它。我翻译XHTML来生成XML,但生成的XML创建了另一个结果。我也会分享一下。 – Vyache

+0

如果你在问题中解释*到底是什么问题,那真的很有帮助。 – Marcin

回答

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> 

您可能需要这个适应您的具体需要。