迭代并将 XSLT 身份转换应用于文档目录? [英] Iterate and apply XSLT identity transformation to directory of documents?

查看:34
本文介绍了迭代并将 XSLT 身份转换应用于文档目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<h1>标题</h1><div class="adv"><div class="warn"><img width="60px" height="20px" src="img/warn.png" alt="警告"></img><p class="cause">gfgfg!</p><p>⇒ Thgfh</p><p class="step">⇔<span class="emphasis">hgfh</p>

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

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

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

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

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

<xsl:result-document method="xhtml" href="new/{$fileName}"><div><h:selectBooleanCheckbox value="pubs"/><xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>

</xsl:result-document></xsl:for-each></xsl:模板><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:模板></xsl:stylesheet>

输出如下:

 

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

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

解决方案

您的样式表似乎缺少 身份转换 模板:

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

另外,为了改变一个属性的值,你需要先重新创建它:

<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:模板>

最后,要处理的 HTML 文档也必须是格式良好的 XML;提供的示例不是.

I have a folder with HTML files which look more or less like this:

<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>

They all have <div id="someId" class="content"> as root element and then just various HTML markup.

I need to change all of the src attributes in the img tags of each document to look like this:

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

and wrap the new output in another div with a new child element. The rest of the document needs to be exactly the same.

I tried this stylesheet, but with this stylesheet only the text nodes get written in the output document (while generating the new div element works):

<?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>

The output looks like this:

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

This is a follow-up to my earlier question, Change attribute value without creating new output document?

解决方案

It looks like your stylesheet is missing the identity transform template:

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

In addition, in order to change the value of an attribute, you need to recreate it first:

<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>

Finally, the HTML document to be processed must also be a well-formed XML; the provided example is not.

这篇关于迭代并将 XSLT 身份转换应用于文档目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆