循环访问 XSLT 标识转换并将其应用于文档目录



我有一个包含HTML文件的文件夹,它们看起来或多或少是这样的:

<div id="d10e3019" class="content">
   <h1>header</h1>
   <div class="adv">
      <div class="warn">
         <img width="60px" height="20px" src="img/warn.png" alt="WARNING"></img>
         <p class="cause">gfgfg!</p>
         <p>⇒  Thgfh</p>
         <p class="step">⇔ 
            <span class="emphasis">hgfh
         </p>
      </div>
   </div>
</div>

它们都有<div id="someId" class="content">作为根元素,然后只是各种HTML标记。

我需要将每个文档的 img 标签中的所有src属性更改为如下所示:

<img width="60px" height="20px" src="http://server.com/{$nameOfTheCurtentFolder}/img/warn.png" alt="WARNING"></img>

并使用新的子元素将新输出包装在另一个div中。文档的其余部分需要完全相同。

我尝试了这个样式表,但使用此样式表时,只有文本节点写入输出文档中(同时生成新的 div 元素有效):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:fn='http://www.w3.org/2005/xpath-functions' exclude-result-prefixes='xsl xs fn' xmlns:h="http://java.sun.com/jsf/html">
    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:param name="files" select="collection('./output?select=*.html')"/>
    <xsl:template match="/">
        <xsl:for-each select="$files">
            <xsl:variable name="fileName" select="tokenize(base-uri(), '/')[last()]"/>
            <xsl:result-document method="xhtml" href="new/{$fileName}">
                <div>
                    <h:selectBooleanCheckbox value="pubs"/>
                    <xsl:copy>
                        <xsl:apply-templates select="@* | node()"/>
                    </xsl:copy>
                </div>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="@src">
        <xsl:variable name="nameOfTheCurtentFolder" select="tokenize(base-uri(), '/')[last()-2]"/>
        <xsl:text>http://server.com/</xsl:text>
        <xsl:value-of select="$nameOfTheCurtentFolder"/>
        <xsl:text>/output/</xsl:text>
        <xsl:value-of select="."/>
    </xsl:template>
</xsl:stylesheet>

输出如下所示:

              <div>
                 <h:selectBooleanCheckbox value="pubs"/>
                  headergfgfg!⇒  Thgfhhgfh
              </div>

这是我之前的问题的后续问题,在不创建新的输出文档的情况下更改属性值?

您的样式表似乎缺少标识转换模板:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

此外,为了更改属性的值,您需要先重新创建它:

<xsl:template match="@src">
    <xsl:attribute name="src">
        <xsl:variable name="nameOfTheCurtentFolder" select="tokenize(base-uri(), '/')[last()-2]"/>
        <xsl:text>http://server.com/</xsl:text>
        <xsl:value-of select="$nameOfTheCurtentFolder"/>
        <xsl:text>/output/</xsl:text>
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

最后,要处理的 HTML 文档也必须是格式正确的 XML;提供的示例不是。

让它工作,也只晚了 5 分钟(与 michael.hor257k 相比):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:fn='http://www.w3.org/2005/xpath-functions' exclude-result-prefixes='xsl xs fn' xmlns:h="http://java.sun.com/jsf/html">
    <xsl:output method="xml" encoding="utf-8"/>
    <xsl:strip-space elements="*"/>
    <xsl:param name="files" select="collection('./output/*.xml')"/>
    <xsl:template match="/">
        <xsl:for-each select="$files">
            <xsl:variable name="fileName" select="tokenize(base-uri(), '/')[last()]"/>
            <xsl:result-document method="xhtml" href="new/{$fileName}">
                <div>
                    <h:selectBooleanCheckbox value="pubs"/>
                    <xsl:copy>
                        <xsl:apply-templates/>
                    </xsl:copy>
                </div>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@src">
        <xsl:attribute name="src">
            <xsl:variable name="nameOfTheCurtentFolder" select="tokenize(base-uri(), '/')[last()-2]"/>
            <xsl:text>http://server.com/</xsl:text>
            <xsl:value-of select="$nameOfTheCurtentFolder"/>
            <xsl:text>/output/</xsl:text>
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

通用解决方案

XSLT 没有迭代文件目录的标准方法。 您需要通过外部控件应用 XSLT 来实现这种效果。 但是,collection()函数的撒克逊扩展可以做到这一点......

循环访问输入文档并应用标识转换

The following XSLT will apply an adjusted identity transformation to all `$inSubdirName` HTML files and place the results in `$outSubdirName`:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:output omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:param name="inSubdirName" select="'in'"/>
  <xsl:param name="outSubdirName" select="'out'"/>
  <!-- Driver for each file in inSubdirName -->
  <xsl:template match="/">
    <xsl:for-each select="collection(concat($inSubdirName, '/?select=*.html'))">
      <xsl:variable name="inFileName" select="base-uri()"/>
      <xsl:variable name="outFileName"
                    select="concat($outSubdirName, '/',
                            tokenize(base-uri(), '/')[last()])"/>
      <xsl:message select="concat('Transforming from ',
                           $inFileName, ' to ', $outFileName)"/>
      <xsl:result-document method="xhtml" href="{$outFileName}">
        <xsl:apply-templates select="@*|node()"/>
      </xsl:result-document>
    </xsl:for-each>
  </xsl:template>
  <!-- Identity transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <!-- Identity transform overrides -->
  <xsl:template match="/div">
    <div xmlns:h="http://java.sun.com/jsf/html">
      <h:selectBooleanCheckbox value="pubs"/>
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </div>
  </xsl:template>
  <xsl:template match="@src">
    <xsl:attribute name="src">
      <xsl:variable name="nameOfTheCurrentFolder"
                    select="tokenize(base-uri(), '/')[last()-2]"/>
      <xsl:text>http://server.com/</xsl:text>
      <xsl:value-of select="$nameOfTheCurrentFolder"/>
      <xsl:text>/output/</xsl:text>
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

当应用于输入 HTML 文件(如您提供的)时,更正为格式正确:

<div id="d10e3019" class="content">
   <h1>header 1</h1>
   <div class="adv">
      <div class="warn">
         <img width="60px" height="20px" src="img/warn.png" alt="WARNING"></img>
         <p class="cause">gfgfg!</p>
         <p>⇒  Thgfh</p>
         <p class="step">⇔ 
            <span class="emphasis">hgfh</span>
         </p>
      </div>
   </div>
</div>

将使用新的 @src 属性和新的 divh:selectBooleanCheckbox 子元素转换它们,

<div xmlns:h="http://java.sun.com/jsf/html">
   <h:selectBooleanCheckbox value="pubs"></h:selectBooleanCheckbox>
   <div id="d10e3019" class="content">
      <h1>header 1</h1>
      <div class="adv">
         <div class="warn">
            <img width="60px" height="20px" src="http://server.com/xslt/output/img/warn.png" alt="WARNING"></img>
            <p class="cause">gfgfg!</p>
            <p>⇒  Thgfh</p>
            <p class="step">⇔ 
               <span class="emphasis">hgfh</span>
            </p>
         </div>
      </div>
   </div>
</div>

根据请求,为 h 命名空间前缀添加声明,以确保输出格式正确。

另请注意对原始 XSLT 的以下改进:

  • 输入和输出目录已参数化。
  • 执行迭代的驱动程序 XSLT 与负责标识转换 XSLT 模板应用更改。

最新更新