如何使用xsl将cdata添加到xml文件
我正在尝试将元素的CDATA打包到xml文件中 我遗漏的另一点是,在添加CDATA时需要转义一些同名元素 这是源xml文件:如何使用xsl将cdata添加到xml文件,xml,xslt,xslt-1.0,xslt-2.0,cdata,Xml,Xslt,Xslt 1.0,Xslt 2.0,Cdata,我正在尝试将元素的CDATA打包到xml文件中 我遗漏的另一点是,在添加CDATA时需要转义一些同名元素 这是源xml文件: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <books> <jndi:binding name="books/cat/action/configs"> <jndi:value type="java.lang.String">
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books>
<jndi:binding name="books/cat/action/configs">
<jndi:value type="java.lang.String">
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
</jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/romance/configs">
<jndi:value type="java.lang.String">
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
</jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/thriller/configs">
<jndi:value type="java.lang.String">
abc.com
</jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/classic/configs">
<jndi:value type="java.lang.String">
abc.com
</jndi:value>
</jndi:binding>
</books>
. 但有些人认为这对我不起作用
这是我尝试的xsl文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jndi="urn:jboss:jndi-binding-service:1.0" >
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
有人可以建议我如何将CDATA添加到元素中,并且需要在CDATA的开头添加一行
以下是预期结果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<books>
<jndi:binding name="books/cat/action/configs">
<jndi:value type="java.lang.String">
<![CDATA[
<?xml version="1.0" encoding="ISO-8859-1"?>
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
]]>
</jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/romance/configs">
<jndi:value type="java.lang.String">
<![CDATA[
<?xml version="1.0" encoding="ISO-8859-1"?>
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"/>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
]]>
</jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/thriller/configs">
<jndi:value type="java.lang.String">
abc.com
</jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/classic/configs">
<jndi:value type="java.lang.String">
abc.com
</jndi:value>
</jndi:binding>
</books>
父域
父域
]]>
父域
父域
]]>
父域
父域
问题在于,您需要将CDATA
部分中的XML转换为文本。此XSLT应完成以下工作:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
<xsl:output method="xml" indent="yes" cdata-section-elements="jndi:value"/>
<xsl:template match="jndi:value">
<xsl:copy>
<!-- Copy the attributes -->
<xsl:apply-templates select="@*"/>
<!-- Convert the contained nodes (elements and text) into text -->
<xsl:variable name="subElementsText">
<xsl:apply-templates select="node()" mode="asText"/>
</xsl:variable>
<!-- Output the XML directive and the converted nodes -->
<xsl:value-of select="concat('<?xml version="1.0" encoding="utf-8"?>',$subElementsText)"/>
</xsl:copy>
</xsl:template>
<!-- Copy text nodes as text -->
<xsl:template match="text()" mode="asText">
<xsl:copy/>
</xsl:template>
<!-- Copy elements as text -->
<xsl:template match="*" mode="asText">
<xsl:value-of select="concat('<',name())"/>
<xsl:for-each select="@*">
<xsl:value-of select="concat(' ',name(),'="',.,'"')"/>
</xsl:for-each>
<xsl:value-of select="'>'"/>
<xsl:apply-templates select="node()" mode="asText"/>
<xsl:value-of select="concat('</',name(),'>')"/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
产生预期的结果:
<?xml version="1.0" encoding="utf-8"?>
<books xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
<jndi:binding name="books/cat/action/configs">
<jndi:value type="java.lang.String"><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"></defaults>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
]]></jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/romance/configs">
<jndi:value type="java.lang.String"><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"></defaults>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
]]></jndi:value>
</jndi:binding>
</book
父域
父域
]]>
父域
父域
]]>
问题是您需要将CDATA
部分中的XML转换为文本。此XSLT应完成以下工作:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
<xsl:output method="xml" indent="yes" cdata-section-elements="jndi:value"/>
<xsl:template match="jndi:value">
<xsl:copy>
<!-- Copy the attributes -->
<xsl:apply-templates select="@*"/>
<!-- Convert the contained nodes (elements and text) into text -->
<xsl:variable name="subElementsText">
<xsl:apply-templates select="node()" mode="asText"/>
</xsl:variable>
<!-- Output the XML directive and the converted nodes -->
<xsl:value-of select="concat('<?xml version="1.0" encoding="utf-8"?>',$subElementsText)"/>
</xsl:copy>
</xsl:template>
<!-- Copy text nodes as text -->
<xsl:template match="text()" mode="asText">
<xsl:copy/>
</xsl:template>
<!-- Copy elements as text -->
<xsl:template match="*" mode="asText">
<xsl:value-of select="concat('<',name())"/>
<xsl:for-each select="@*">
<xsl:value-of select="concat(' ',name(),'="',.,'"')"/>
</xsl:for-each>
<xsl:value-of select="'>'"/>
<xsl:apply-templates select="node()" mode="asText"/>
<xsl:value-of select="concat('</',name(),'>')"/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
产生预期的结果:
<?xml version="1.0" encoding="utf-8"?>
<books xmlns:jndi="urn:jboss:jndi-binding-service:1.0">
<jndi:binding name="books/cat/action/configs">
<jndi:value type="java.lang.String"><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"></defaults>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
]]></jndi:value>
</jndi:binding>
<jndi:binding name="books/cat/romance/configs">
<jndi:value type="java.lang.String"><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<urlConfig>
<defaults catID="1983" subcatID="1987" method="get" onError="keep"></defaults>
<urlKey name="logo" altURL="def.com">
<address>abc.com</address>
</urlKey>
<urlKey name="logo1" altURL="def.com">
<address>abc.com</address>
</urlKey>
</urlConfig>
]]></jndi:value>
</jndi:binding>
</book
父域
父域
]]>
父域
父域
]]>
您好@Mimo,谢谢您的代码,我没有看到预期的输出,不知道如何在元素中包含
。我得到了正确的输出-请参见上文。XSLT已经添加了XML指令。感谢您提供的详细信息,今天早上我忘了提到,我还需要从添加CDATA中跳过一些具有相同模板匹配“jndi:value”的元素。请检查更新的源文件和输出。据我所知,您不能使用XSLT生成相同的元素,有时是CDATA内容,有时不是。不过,让这些元素包含或不包含CDATA内容应该不会有任何区别:归根结底,CDATA只是简化编码的一种方式,但XML解析器将加载的文本完全相同。是否可以将需要CDATA的元素和不需要CDATA的元素分组。示例:如果父元素的名称为
请向该元素添加CDATA,否则不要对该元素执行任何CDATA。您好@Mimo,感谢您的代码,我没有看到预期的输出,不知道如何将
包含在元素中。我得到了正确的输出-请参见上文。XSLT已经添加了XML指令。感谢您提供的详细信息,今天早上我忘了提到,我还需要从添加CDATA中跳过一些具有相同模板匹配“jndi:value”的元素。请检查更新的源文件和输出。据我所知,您不能使用XSLT生成相同的元素,有时是CDATA内容,有时不是。不过,让这些元素包含或不包含CDATA内容应该不会有任何区别:归根结底,CDATA只是简化编码的一种方式,但XML解析器将加载的文本完全相同。是否可以将需要CDATA的元素和不需要CDATA的元素分组。示例:如果父元素的名称为
请向该元素添加CDATA,否则不要为该元素执行任何CDATA。命名空间URI为:xmlns:jndi=“urn:jboss:jndi绑定服务:1.0”命名空间URI为:xmlns:jndi=“urn:jboss:jndi绑定服务:1.0”