Xml 使用xsl从外部文档替换变量
我试图用properties.xml中的值替换源文件中的变量Xml 使用xsl从外部文档替换变量,xml,xslt,replace,xslt-1.0,xslt-2.0,Xml,Xslt,Replace,Xslt 1.0,Xslt 2.0,我试图用properties.xml中的值替换源文件中的变量@mybook.01@,该值与properties.xml中的组id匹配源文件中的图书名称元素 这是我的源文件: <books> <us-country-factory> <book-name>books/props/Classic</book-name> <store-property name="book1" type="java.lang.Stri
@mybook.01@
,该值与properties.xml中的组id
匹配源文件中的图书名称
元素
这是我的源文件:
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2">CLIENT</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">@mybook.01@</store-property>
<store-property name="book2" type="java.lang.String">@mybook.01@</store-property>
</us-country-factory>
</books>
你可以做类似的事情来代替 XSLT2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="props" select="document('properties.xml')"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="store-property[matches(.,'^@')]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="$props/*/group[@id=current()/../book-name]/variable[@id=current()/@name]/*[local-name()=tokenize(current(),'@')[2]]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML输出(使用提供的XML示例)
书籍/道具/经典
123
789
客户
书籍/道具/经典1
ab2
rt67
请提供建议…当我添加其他子节点时,客户端
提供的代码@Daniel输出不好。感谢您的帮助。。代码是有效的。只是出于好奇,想知道这两者之间的区别是什么:``和``@phani-其实不多。你可以用任何一个。另外,我修改了样式表以处理您对输入XML所做的更新。现在输出附加的存储属性
。如果您不希望它输出,只需添加此模板:
我注意到这两种逻辑之间的区别是:我发布的一种逻辑还支持其他子节点属性,其中您提供的以下逻辑仅支持其中包含存储名
的子节点。谢谢你的帮助。
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">123</store-property>
<store-property name="book2" type="java.lang.String">789</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">ab2</store-property>
<store-property name="book2" type="java.lang.String">rt67</store-property>
</us-country-factory>
</books>
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:key name="props" match="variable/*"
use="concat(../@id,'
',name(.))"/>
<xsl:template match="book-name">
<xsl:apply-templates select="store-property"/>
</xsl:template>
<xsl:template match="store-property">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="id" select="@name"/>
<xsl:analyze-string select="." regex="@(.*?)@">
<xsl:matching-substring>
<xsl:value-of select="key('props',concat($id,'
',regex-group(1)),
doc('properties.xml'))"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<!--identity for all other nodes-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="props" select="document('properties.xml')"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="store-property[matches(.,'^@')]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="$props/*/group[@id=current()/../book-name]/variable[@id=current()/@name]/*[local-name()=tokenize(current(),'@')[2]]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<books>
<us-country-factory>
<book-name>books/props/Classic</book-name>
<store-property name="book1" type="java.lang.String">123</store-property>
<store-property name="book2" type="java.lang.String">789</store-property>
<store-property name="book2">CLIENT</store-property>
</us-country-factory>
<us-country-factory>
<book-name>books/props/Classic1</book-name>
<store-property name="book1" type="java.lang.String">ab2</store-property>
<store-property name="book2" type="java.lang.String">rt67</store-property>
</us-country-factory>
</books>