Xslt 基于输入参数应用模板

Xslt 基于输入参数应用模板,xslt,Xslt,我需要根据给定的输入参数应用模板 输入XML: <?xml version="1.0"?> <chapter xmlns="http://www.w3.org/1998/Math/MathML"> <section1> <math><mtext>This is section1 mtext</mtext></math> </section1> <secti

我需要根据给定的输入参数应用模板

输入XML:

<?xml version="1.0"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
    <section1>
        <math><mtext>This is section1 mtext</mtext></math>
    </section1>
    <section2>
        <math><mtext>This is section2 mtext</mtext></math>
    </section2>
</chapter>

这是第一节mtext
这是第二节mtext
如果用户提供一个Xpath表达式作为输入参数,那么应该选择特定的Xpath并应用模板

例如,如果用户将“/chapter/section1/”作为输入参数,则需要输出

<?xml version="1.0" encoding="UTF-8"?><chapter>
<section1>
<math>~rom1~This is section1 mtext</math>
</section1>
<section2>
<math>~rom~this is section2 mtext</math>
</section2>
</chapter>

~rom1~这是第一节的mtext
~rom~这是第二节mtext
如果用户提供“/chapter/section1/”和“/chapter/section2”作为两个输入参数,则输出应为

<?xml version="1.0" encoding="UTF-8"?><chapter>
<section1>
<math>~rom1~This is section1 mtext</math>
</section1>
<section2>
<math>~rom2~this is section2 mtext</math>
</section2>
</chapter>

~rom1~这是第一节的mtext
~rom2~这是第二节的mtext
如果用户没有给出任何参数,那么输出应该是

<?xml version="1.0" encoding="UTF-8"?><chapter>
<section1>
<math>~rom~This is section1 mtext</math>
</section1>
<section2>
<math>~rom~this is section2 mtext</math>
</section2>
</chapter>

~rom~这是第一节mtext
~rom~这是第二节mtext
我尝试了下面的XSLT

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML">
<xsl:param name="Xpath1"/>
<xsl:param name="Xpath2"/>
<xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:template>
<xsl:template match="Xpath1//mtext"><xsl:text>~rom1~</xsl:text><xsl:apply-templates/></xsl:template>
<xsl:template match="Xpath2//mtext"><xsl:text>~rom2~</xsl:text><xsl:apply-templates/></xsl:template>
<xsl:template match="m:mtext"><xsl:text>~rom~</xsl:text><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>

~rom1~
~rom2~
~rom~

如果这是不可能的,请建议一种替代方法来实现这一点

您使用哪种XSLT处理器?纯XSLT 1.0或2.0不支持将字符串作为XPath表达式进行动态计算,但如果您使用的是Saxon,则当前XSLT 3.0草案中提供了支持,或者使用特定于处理器的扩展。@Ian Roberts:感谢您的快速回复。我使用的是萨克森处理器。我可以使用XSLT2.0。这是通过其他方式实现的吗?您需要一个xpath表达式作为参数还是节点名称足够(例如section1)?@hr_117:我需要一个xpath表达式作为参数您使用哪一版本的Saxon 9?商业支持。