我正在尝试编写我的第一个XSLT.它需要找到bind属性ref以"$ .root"开头的所有元素,然后插入".newRoot".我已设法匹配特定属性,但我不明白如何让它来打印更新的属性值.
输入示例XML:
<?xml version="1.0" encoding="utf-8" ?>
<top>
<products>
<product>
<bind ref="$.root.other0"/>
</product>
<product>
<bind ref="$.other1"/>
</product>
<product>
<bind ref="$.other2"/>
</product>
<product>
<bind ref="$.root.other3"/>
</product>
</products>
</top>
Run Code Online (Sandbox Code Playgroud)
我的XSL到目前为止:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref">
<xsl:attribute name="ref">$.newRoot<xsl:value-of select="bind/@ref" /></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
我想从输入中生成的XML:
<?xml version="1.0" encoding="utf-8" ?>
<top>
<products>
<product>
<bind ref="$.newRoot.root.other0"/>
</product>
<product>
<bind ref="$.other1"/>
</product>
<product>
<bind ref="$.other2"/>
</product>
<product>
<bind ref="$.newRoot.root.other3"/>
</product>
</products>
</top>
Run Code Online (Sandbox Code Playgroud)
代替:
<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref">
<xsl:attribute name="ref">$.newRoot<xsl:value-of select="bind/@ref" /></xsl:attribute>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
尝试:
<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref">
<xsl:attribute name="ref">$.newRoot.root<xsl:value-of select="substring-after(., '$.root')" /></xsl:attribute>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
或(更方便的语法相同):
<xsl:template match="bind/@ref[starts-with(., '$.root')]">
<xsl:attribute name="ref">
<xsl:text>$.newRoot.root</xsl:text>
<xsl:value-of select="substring-after(., '$.root')" />
</xsl:attribute>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
注意.用于引用当前节点.在您的版本中,<xsl:value-of select="bind/@ref" />指令不会选择任何内容,因为该ref属性已经是当前节点 - 并且它没有子节点.
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |