代码之家  ›  专栏  ›  技术社区  ›  Ace

符合XSL特定属性

  •  1
  • Ace  · 技术社区  · 15 年前

    我有一堆docbook文件,它们的imagedata上有不同的属性。我想让它们都有一个唯一的属性和三个相同的属性:

      <section xmlns="http://docbook.org/ns/docbook" version="5" xml:id="cancelDia">
        <title>Screenshot</title>
        <mediaobject>
          <imageobject>
            <imagedata fileref="screenshots/cancelDialog.png" scalefit="1" width="100%" contentdepth="100%"/>
          </imageobject>
        </mediaobject>
      </section>
    

    fileref是唯一的,应该单独使用,但是scalefit、width和contentdepth在所有的 <imagedata> . 一个问题是,大多数图像数据具有scalefit,少数具有width,少数具有contentdepth。我如何确保,即使他们已经有了这个属性,我的 <图像数据> 具有相同的刻度、宽度和内容深度?

    注:我不确定这是否重要,但我正在使用docbook 5

    2 回复  |  直到 7 年前
        1
  •  4
  •   user357812user357812    15 年前

    此样式表:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:doc="http://docbook.org/ns/docbook"
     xmlns="http://docbook.org/ns/docbook"
     exclude-result-prefixes="doc">
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="doc:imagedata">
            <imagedata fileref="{@fileref}"
                           scalefit="1" width="100%" contentdepth="100%"/>
        </xsl:template>
    </xsl:stylesheet>
    

    输出:

    <section version="5" xml:id="cancelDia" xmlns="http://docbook.org/ns/docbook">
        <title>Screenshot</title>
        <mediaobject>
            <imageobject>
                <imagedata fileref="screenshots/cancelDialog.png" 
                           scalefit="1" width="100%" contentdepth="100%" />
            </imageobject>
        </mediaobject>
    </section>
    

    编辑 :匹配新输入样本。

        2
  •  1
  •   Dimitre Novatchev    15 年前

    这种转变 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:d="http://docbook.org/ns/docbook"
     xmlns="http://docbook.org/ns/docbook"
     xmlns:ext="http://exslt.org/common"
     exclude-result-prefixes="d ext">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pAttribs">
      <p scalefit="1" width="100%" contentdepth="100%"/>
     </xsl:param>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="d:imagedata">
      <xsl:copy>
       <xsl:copy-of select="@fileref
        |
         ext:node-set($pAttribs)/*/@*"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    当应用于所提供的XML文档时 :

    <section xmlns="http://docbook.org/ns/docbook"
             version="5" xml:id="cancelDia">
        <title>Screenshot</title>
        <mediaobject>
            <imageobject>
                <imagedata fileref="screenshots/cancelDialog.png"
                 scalefit="1"
                 width="100%"
                 contentdepth="100%"/>
            </imageobject>
        </mediaobject>
    </section>
    

    产生想要的正确结果 :

    <section xmlns="http://docbook.org/ns/docbook"
             version="5" xml:id="cancelDia">
       <title>Screenshot</title>
       <mediaobject>
          <imageobject>
             <imagedata fileref="screenshots/cancelDialog.png" 
                        scalefit="1" width="100%" contentdepth="100%"/>
          </imageobject>
       </mediaobject>
    </section>
    

    做笔记 :

    所有所需的值都设置为外部元素内部元素的属性 <xsl:param>