我正在寻找一种使用xslt替换xml文件中字段值的方法。除命名空间前缀外,一切都很好用。在下面的源文件中,我想更改密码元素值。
<?xml version="1.0" encoding="utf-8"?>
<ns0:MYXML xmlns:ns0="http://www.me.com/myxml" xmlns="http://www.me.com/myxml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:Header>
<ns0:Infromation>
<ns0:From>
<ns0:Credential>
<ns0:User>jeff</ns0:User>
<Password xmlns="">OLD VALUE</Password>
</ns0:Credential>
</ns0:From>
</ns0:Infromation>
<ns0:Misc>
<ns0:ID>1002</ns0:ID>
<ns0:Timestamp>2012-01-16T09:23:33</ns0:Timestamp>
<ns0:Type>unknown</ns0:Type>
</ns0:Misc>
<ns0:State>
<ns0:ConversationId>d66d9304-9025-a580-e111-5640bf36560d</ns0:ConversationId>
</ns0:State>
</ns0:Header>
</ns0:MYXML>
这是我的结果:
<?xml version="1.0" encoding="utf-8"?>
<ns0:MYXML xmlns:ns0="http://www.me.com/myxml" xmlns="http://www.me.com/myxml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:Header>
<ns0:Infromation>
<ns0:From>
<ns0:Credential>
<ns0:User>jeff</ns0:User>
<Password xmlns="">New Value</Password>
</ns0:Credential>
</ns0:From>
</ns0:Infromation>
<ns0:Misc>
<ns0:ID>1002</ns0:ID>
<ns0:Timestamp>2012-01-16T09:23:33</ns0:Timestamp>
<ns0:Type>unknown</ns0:Type>
</ns0:Misc>
<ns0:State>
<ns0:ConversationId>d66d9304-9025-a580-e111-5640bf36560d</ns0:ConversationId>
</ns0:State>
</ns0:Header>
</ns0:MYXML>
这就是我用于转换的xslt代码:
<?xml version="1.0" encoding="utf-8"?>
<ns0:MYXML xmlns:ns0="http://www.me.com/myxml" xmlns="http://www.me.com/myxml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:Header>
<ns0:Infromation>
<ns0:From>
<ns0:Credential>
<ns0:User>jeff</ns0:User>
<Password xmlns="">New Value</Password>
</ns0:Credential>
</ns0:From>
</ns0:Infromation>
<ns0:Misc>
<ns0:ID>1002</ns0:ID>
<ns0:Timestamp>2012-01-16T09:23:33</ns0:Timestamp>
<ns0:Type>unknown</ns0:Type>
</ns0:Misc>
<ns0:State>
<ns0:ConversationId>d66d9304-9025-a580-e111-5640bf36560d</ns0:ConversationId>
</ns0:State>
</ns0:Header>
</ns0:MYXML>
我尝试使用“{name()}”作为元素名称,但这会抛出命名空间不存在的异常。
非常感谢任何帮助!
答案 0 :(得分:0)
我不清楚您是否希望Password元素属于原始Xml中显示的空命名空间,或者您希望它位于http://www.me.com/myxml命名空间中。这是一个Xslt样式表,它使用http://www.me.com/myxml命名空间中的Password元素替换空命名空间中的Password元素(我使用了ns0前缀,使其看起来像Xml文档中的其他元素):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Password">
<ns0:Password xmlns:ns0="http://www.me.com/myxml">
<xsl:copy-of select="@*"/>
<xsl:text>New Value</xsl:text>
</ns0:Password>
</xsl:template>
<!-- This creates the element in the empty namespace
<xsl:template match="Password">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:text>New Value</xsl:text>
</xsl:copy>
</xsl:template>
-->
</xsl:stylesheet>
我还包含了一个模板,用于复制现有的Password元素并替换其值以备不时之需。如果您不需要它,您可以完全移除该块。 这是转换结果:
<?xml version="1.0" encoding="utf-8"?>
<ns0:MYXML xmlns:ns0="http://www.me.com/myxml" xmlns="http://www.me.com/myxml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns0:Header>
<ns0:Infromation>
<ns0:From>
<ns0:Credential>
<ns0:User>jeff</ns0:User>
<ns0:Password>New Value</ns0:Password>
</ns0:Credential>
</ns0:From>
</ns0:Infromation>
<ns0:Misc>
<ns0:ID>1002</ns0:ID>
<ns0:Timestamp>2012-01-16T09:23:33</ns0:Timestamp>
<ns0:Type>unknown</ns0:Type>
</ns0:Misc>
<ns0:State>
<ns0:ConversationId>d66d9304-9025-a580-e111-5640bf36560d</ns0:ConversationId>
</ns0:State>
</ns0:Header>
</ns0:MYXML>
的Pawel