如何使用 XSL 删除原始 XML 文档中的命名空间并仅保留部分元素?
下面是我的 XML。我想使用 XSL 来解析它。我想要实现的是删除命名空间(xmlns),然后只保留一些元素及其属性。我找到了一种删除名称空间的方法,但是当我将其与保留某些元素的代码放在一起时,它不起作用。我已经尝试过身份但仍然不起作用。
我希望有人可以分享一些东西。预先非常感谢您。
XML 输入:
<Transaction xmlns="http://www.test.com/rdc.xsd">
<Transaction>
<StoreName id="aa">STORE A</StoreName>
<TransNo>TXN0001</TransNo>
<RegisterNo>REG001</RegisterNo>
<Items>
<Item id="1">
<ItemID>A001</ItemID>
<ItemDesc>Keychain</ItemDesc>
</Item>
<Item id="2">
<ItemID>A002</ItemID>
<ItemDesc>Wallet</ItemDesc>
</Item>
</Items>
<IDONTLIKETHIS_1>
<STOREXXX>XXX-</STOREXXX>
<TRANSXXX>YYY</TRANSXXX>
</IDONTLIKETHIS_1>
<IDONTLIKETHIS_2>
<STOREXXX>XXX-</STOREXXX>
<TRANSXXX>YYY</TRANSXXX>
</IDONTLIKETHIS_2>
</Transaction>
<Transaction>
预期 XML 输出:
<Transaction>
<Transaction>
<StoreName id="aa">STORE A</StoreName>
<TransNo>TXN0001</TransNo>
<RegisterNo>REG001</RegisterNo>
<Items>
<Item id="1">
<ItemID>A001</ItemID>
<ItemDesc>Keychain</ItemDesc>
</Item>
<Item id="2">
<ItemID>A002</ItemID>
<ItemDesc>Wallet</ItemDesc>
</Item>
</Items>
</Transaction>
<Transaction>
用于删除命名空间 (xmlns) 的代码:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
Below is my XML. I wanted to parse this using XSL. What I want to achieve is to remove the namespace (xmlns) then just retain some of the elements and their attributes. I found a way to remove the namespace but when I put it together with the code to retain some of the elements, it doesn't work. I already tried the identity but still didn't work.
I hope someone out there could share something. Thank you very much in advance.
XML Input:
<Transaction xmlns="http://www.test.com/rdc.xsd">
<Transaction>
<StoreName id="aa">STORE A</StoreName>
<TransNo>TXN0001</TransNo>
<RegisterNo>REG001</RegisterNo>
<Items>
<Item id="1">
<ItemID>A001</ItemID>
<ItemDesc>Keychain</ItemDesc>
</Item>
<Item id="2">
<ItemID>A002</ItemID>
<ItemDesc>Wallet</ItemDesc>
</Item>
</Items>
<IDONTLIKETHIS_1>
<STOREXXX>XXX-</STOREXXX>
<TRANSXXX>YYY</TRANSXXX>
</IDONTLIKETHIS_1>
<IDONTLIKETHIS_2>
<STOREXXX>XXX-</STOREXXX>
<TRANSXXX>YYY</TRANSXXX>
</IDONTLIKETHIS_2>
</Transaction>
<Transaction>
Expected XML Output:
<Transaction>
<Transaction>
<StoreName id="aa">STORE A</StoreName>
<TransNo>TXN0001</TransNo>
<RegisterNo>REG001</RegisterNo>
<Items>
<Item id="1">
<ItemID>A001</ItemID>
<ItemDesc>Keychain</ItemDesc>
</Item>
<Item id="2">
<ItemID>A002</ItemID>
<ItemDesc>Wallet</ItemDesc>
</Item>
</Items>
</Transaction>
<Transaction>
Code used to remove the namespace (xmlns):
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明:
匹配“*”的模板匹配任何元素并重新创建它(
)具有相同的名称,但不复制任何命名空间节点。它还复制该元素的所有属性。然后,它将模板(包括其自身 - 递归地)应用到该元素的所有子节点上 - 不仅是元素,而且是所有类型的子节点:元素、文本节点、处理指令和注释。最后一个模板与我们不希望复制到输出的任何节点相匹配,并且使用空模板主体来完成此操作(不复制)。
第二个模板匹配所有非元素节点并将其复制到输出,不包括文档节点
/
Explanation:
The template matching "*" matches any element and recreates it (
<xsl:element>
) with the same name, but doesn't copy any namespace nodes. It also copies all attributes of this element. Then it applies templates (including itself -- recursively) on all of this element's children nodes -- not only elements but all types of children nodes: elements, text nodes, processing-instructions and comments.The last template matches any node we don't like to copy to the output and does exactly this (no copying ) with an empty template body.
The second template matches and copies to the output all nodes that aren't elements, excluding the document node
/