[英]Iterate and apply XSLT identity transformation to directory of documents?
我有一个包含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()
函数进行Saxon扩展,从而可以做到这一点...
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
属性,新的div
和h: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的以下改进:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.