简体   繁体   English

XSLT:将xml:id属性添加到顶级节点

[英]XSLT: Add an xml:id attribute to the top-level node

I'd like to add the xml:id=foo attribute to the top-level <book> node in a DocBook file, using XSLT. 我想使用XSLT将xml:id=foo属性添加到DocBook文件中的顶级<book>节点。 I've got something working, but I was wondering if there was a simpler way to implement this. 我有一些工作,但我想知道是否有更简单的方法来实现这一点。 Here's my current solution: 这是我当前的解决方案:

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                xmlns:db='http://docbook.org/ns/docbook'
                version='1.0'>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="db:book">
    <xsl:copy>
        <xsl:attribute name="xml:id">
            <xsl:text>foo</xsl:text>
        </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

You could shorten 你可以缩短

    <xsl:attribute name="xml:id">
        <xsl:text>foo</xsl:text>
    </xsl:attribute>

to

    <xsl:attribute name="xml:id">foo</xsl:attribute>

But other than that your approach is fine. 但是除此之外,您的方法还可以。 And your version might be preferred for readability. 并且您的版本可能更易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 撒克逊人为XSLT抛出“顶级文本节点”异常,但XML对我而言似乎格式正确 - Saxon throws 'top-level text node' exception for XSLT, but XML seems well-formed to me 如何在 SQL Server 中选择 XML 列的顶级属性? - How do I select a top-level attribute of an XML column in SQL Server? 获取XQuery [modify()]:修改xml时不支持顶级属性节点错误 - Getting XQuery [modify()]: Top-level attribute nodes are not supported error while modifying xml XML:为什么我的DOM遍历功能只产生顶级节点? - XML: why is my DOM traversal function yielding only the top-level node? 在 xslt 中合并 xml 文件时添加一次顶级标签 - add top level tag once while merging xml files in xslt 如何使用XSLT将顶级元素添加到XML? - How to add top level element to XML using XSLT? XSLT,将属性添加到生成的 xml 文件的根节点 - XSLT, Add an attribute to the root node of the resultant xml file 如何使用 xslt 将节点/元素作为属性添加到 XML 节点 - How to add nodes/elements as attribute to XML node using xslt Golang在xml结构中指定顶级标签 - Golang specify top-level tag in xml structure XML Schema 1.0中以顶级祖先为条件的后代约束 - descendant constraint conditional on top-level ancestor in XML Schema 1.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM