首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在xsl:apply-模板中使用变量作为值?

如何在xsl:apply-模板中使用变量作为值?
EN

Stack Overflow用户
提问于 2020-11-04 07:00:33
回答 1查看 262关注 0票数 0

我试图从xml文件中提取一些数据,并根据特定的节点将其传递给许多html文件。我的source.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8" ?>
<products>
    <product>
        <id>1</id>
        <feature>Product description escaped html tags</feature>
    </product>
    <product>
        <id>2</id>
        <feature>Product description escaped html tags</feature>
    </product>
.........................
    <product>
        <id>5</id>
        <feature>Product description escaped html tags</feature>
    </product>
</products>

预期结果:具有以下内容的多个html文件:

代码语言:javascript
运行
复制
<p>1</p>
Product description with html tags

我正在使用这个python代码:

代码语言:javascript
运行
复制
import lxml.etree as ET

doc = ET.parse('source.xml')
xslt = ET.parse('modify.xsl')
transform = ET.XSLT(xslt)

products = doc.findall('product')

for product in products:
    i = product.find('id')
    n = ET.XSLT.strparam(str(i))
    describ = transform(doc, item_num=n)
    f = open('file_nr_{}.html'.format(i.text), 'wb')
    f.write(describ)
    f.close()

我当前的样式表modify.xsl

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="UTF-8" />

    <xsl:param name="item_num"/>

    <xsl:template match="/products">
        <xsl:apply-templates select="product[id=$item_num]" />
    </xsl:template>

    <xsl:template match="id" >
        <p><xsl:value-of select="."/></p>
        <xsl:value-of select="following-sibling::feature" disable-output-escaping="yes"/>
    </xsl:template>
  </xsl:stylesheet>

..gives me多个完整的空零字节文件。但当我换行时:

代码语言:javascript
运行
复制
<xsl:apply-templates select="product[id=$item_num]" />

对此:

代码语言:javascript
运行
复制
<xsl:apply-templates select="product[id&lt;4]" />

它给了我五个内容相同的文件:

代码语言:javascript
运行
复制
<p>1</p>Product description with still escaped html tags
Product description with still escaped html tags

<p>2</p>Product description with still escaped html tags
Product description with still escaped html tags

<p>3</p>Product description with still escaped html tags
Product description with still escaped html tags

我不知道如何:

  • 在路径匹配中正确使用变量,只有一个具有特定<product><id>
  • 有效失能逃逸
  • 让谷歌找到解决方案..。;)

我尝试了,并搜索了这里,但我不能在我的案例中使用这些知识。可能是因为我仍然不明白传递变量值是如何工作的。从周五开始,我一直在独自解决这个问题,但我唯一的结果就是头痛。请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-04 07:36:01

你的尝试:

代码语言:javascript
运行
复制
<xsl:template match="/products">
    <xsl:apply-templates select="product[id=$item_num]" />
</xsl:template>

<xsl:template match="id" >
    <p><xsl:value-of select="."/></p>
    <xsl:value-of select="following-sibling::feature" disable-output-escaping="yes"/>
</xsl:template>

将模板应用于<product>节点,但具有<id>节点的模板。

<product>节点创建一个模板。

代码语言:javascript
运行
复制
<xsl:template match="product">
    <p><xsl:value-of select="id"/></p>
    <xsl:value-of select="feature" disable-output-escaping="yes" />
</xsl:template>

每当XSLT找不到特定节点的模板时,它就会回到默认行为。默认行为是“复制子文本节点以输出,并将模板应用于子元素”。这就解释了为什么您看到了代码所看到的内容。

关于另一个问题:.find('...')返回XML节点,而不是字符串值,即.find('id')查找<id>元素。您希望将查找节点的.text作为XSLT参数传递,而不是节点本身:

代码语言:javascript
运行
复制
import lxml.etree as ET

doc = ET.parse('source.xml')
xslt = ET.parse('modify.xsl')
transform = ET.XSLT(xslt)

products = doc.findall('product')

for product in products:
    i = product.find('id').text
    describ = transform(doc, item_num=i)
    with open(f'file_nr_{i}.html', 'wb') as f:
        f.write(describ)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64675440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档