Java 将带有参数的XSLT文件转换为另一个XSLT文件
这里我有一个XSLT文件,其中一些XPath值来自外部。例如,我有一个参数:Java 将带有参数的XSLT文件转换为另一个XSLT文件,java,xml,xslt,xpath,Java,Xml,Xslt,Xpath,这里我有一个XSLT文件,其中一些XPath值来自外部。例如,我有一个参数: <xsl:param name="companyName" select="''"/> 我怎么能这么做?请帮帮我。非常感谢你 p/s:我使用Java *编辑* 简而言之,我想要的是: 在源文件中: <xsl:param name="companyName" select="''"/> <xsl:template name="start"> <xsl:element n
<xsl:param name="companyName" select="''"/>
我怎么能这么做?请帮帮我。非常感谢你
p/s:我使用Java
*编辑*
简而言之,我想要的是:
在源文件中:
<xsl:param name="companyName" select="''"/>
<xsl:template name="start">
<xsl:element name="Promotions">
<xsl:attribute name="CompanyName">
<xsl:value-of select="$companyName"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
XSLT样式表是一个XML文档,这与将XML转换为XML的任何其他问题没有区别。在这种情况下,您似乎希望保持大部分输入文件(即初始样式表)不变,但用固定值替换任何输入文件。这可以通过基于标识模板的常规方法来实现,该方法将所有输入复制到输出,除非被更具体的模板覆盖。在本例中,更具体的模板是与元素的特定值匹配的模板 这将转换输入样式表,例如
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="companyName" select="''" />
<xsl:template match="/">
<xsl:element name="Promotions">
<xsl:attribute name="CompanyName">
<xsl:value-of select="$companyName"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
进入
它给出了完全相同的行为,但公司名称现在是硬编码的,而不是参数化的。我选择生成值,而不是考虑值可能包含单引号字符的可能性,例如Sainsbury的-使用文本内容通常被认为效率较低,因为它将生成一个额外的节点,但当值包含单引号或双引号时,它可以处理,甚至两种类型都在同一个字符串中。您发现问题的哪一部分比较困难?生成XSLT就像生成任何其他XML文档一样。如果只是因为xsl:namespace别名的示例通常与生成样式表有关,那么您可能想阅读一下xsl:namespace别名声明。谢谢您的评论。最好的方法是什么?XSLT+XSLT=使用transformer的新XSLT?抱歉,我无法为您设计一个只有一行需求信息的程序。我已经编辑了我的问题。这对你来说够了吗?谢谢你的明确回答。我完全理解。我试试看。如果它运行良好,我会将此标记为答案。这里有一个问题。我还将参数放在match属性中,但我无法编写以下内容:。您有什么解决方案吗?@Forte_201092 XSLT 1.0不允许在匹配表达式中使用变量引用。XSLT2.0确实如此,但与您的示例建议的方式不同,$start的内容被视为匹配模式。请参阅我编辑的部分。我试过一些方法,但总是在我的控制之下。剩下的没问题。我不知道如何摆脱它。我做到了!:D谢谢你的帮助!
<xsl:param name="companyName" select="''"/>
<xsl:template name="start">
<xsl:element name="Promotions">
<xsl:attribute name="CompanyName">
<xsl:value-of select="$companyName"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="myStart">
<xsl:element name="Promotions">
<xsl:attribute name="CompanyName">abc</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- treat whitespace the same as an XSLT processor would - ignore text nodes
containing only whitespace, except within xsl:text -->
<xsl:strip-space elements="*" />
<xsl:preserve-space elements="xsl:text" />
<xsl:output method="xml" indent="yes" />
<!-- the parameter you describe in the question -->
<xsl:param name="companyName" select="''"/>
<!-- the identity template -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<!-- handling for the specific value-of element -->
<xsl:template match="xsl:value-of[@select = '$companyName']">
<xsl:value-of select="$companyName" />
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<!-- the parameter you describe in the question -->
<xsl:param name="companyName" select="''"/>
<!-- the identity template -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<!-- handling for the specific param element -->
<xsl:template match="/xsl:stylesheet/xsl:param[@name = 'companyName']">
<xsl:element name="xsl:variable">
<xsl:attribute name="name">companyName</xsl:attribute>
<xsl:value-of select="$companyName" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="companyName" select="''" />
<xsl:template match="/">
<xsl:element name="Promotions">
<xsl:attribute name="CompanyName">
<xsl:value-of select="$companyName"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="companyName">abc</xsl:variable>
<xsl:template match="/">
<xsl:element name="Promotions">
<xsl:attribute name="CompanyName">
<xsl:value-of select="$companyName"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>