Xml XSLT-提取节点的属性值,该节点有一个子元素,该子元素有一个目标属性值和元素值?
首先,我为这个复杂的标题道歉,但我希望我发布的示例信息能让我的问题更清楚 我有一个如下所示的XML文档:Xml XSLT-提取节点的属性值,该节点有一个子元素,该子元素有一个目标属性值和元素值?,xml,xslt,Xml,Xslt,首先,我为这个复杂的标题道歉,但我希望我发布的示例信息能让我的问题更清楚 我有一个如下所示的XML文档: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Setting Name="LDAPS
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Setting Name="LDAPS" Type="htf:map">
<Setting Name="LDAP-A" Type="htf:map">
<Setting Name="MY_URL" Type="xsd:string">ldap://xxxx:yyy</Setting>
<Setting Name="Name" Type="xsd:string">MY_LDAP1</Setting>
<Setting Name="somethingelse" Type="xsd:string">SOMEOTHERVALUE1</Setting>
</Setting>
<Setting Name="LDAP-B" Type="htf:map">
<Setting Name="MY_URL" Type="xsd:string">ldap:abc:xyz</Setting>
<Setting Name="Name" Type="xsd:string">MY_TARGET_LDAP</Setting>
<Setting Name="somethingelse" Type="xsd:string">SOMEOTHERVALUE2</Setting>
<Setting Name="somethingnew" Type="xsd:string">SOMENEWVALUE</Setting>
</Setting>
<Setting Name="LDAP-C" Type="htf:map">
<Setting Name="MY_URL" Type="xsd:string">ldap:abc:xxxx</Setting>
<Setting Name="Name" Type="xsd:string">MY_LDAP3</Setting>
<Setting Name="somethingelse" Type="xsd:string">SOMEOTHERVALUE3</Setting>
<Setting Name="somethingelseotherdata" Type="xsd:string">SOMEOTHERDATAVALUE3</Setting>
</Setting>
</Setting>
</Configuration>
我正在尝试使用XSLT完成大部分工作,到目前为止,我拥有以下XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="/Configuration/Setting">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/Configuration/Setting/Setting">
<ldapname> <xsl:value-of select="./@Name"/> </ldapname>
<xsl:for-each select="*">
<response>
<respname> <xsl:value-of select="./@Name"/> </respname>
<respvalue><xsl:value-of select="."/> </respvalue>
</response> <xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
;
我在Redhat机器上使用xsltproc进行测试,当我针对示例XML运行该测试时,我得到:
xsltproc generic1.xsl generic-test.xml:
<html xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <body>
<ldapname>LDAP-A</ldapname><response><respname>MY_URL</respname><respvalue>ldap://xxxx:yyy</respvalue></response>
<response><respname>Name</respname><respvalue>MY_LDAP1</respvalue></response>
<response><respname>somethingelse</respname><respvalue>SOMEOTHERVALUE1</respvalue></response>
<ldapname>LDAP-B</ldapname><response><respname>MY_URL</respname><respvalue>ldap:abc:xyz</respvalue></response>
<response><respname>Name</respname><respvalue>MY_TARGET_LDAP</respvalue></response>
<response><respname>somethingelse</respname><respvalue>SOMEOTHERVALUE2</respvalue></response>
<response><respname>somethingnew</respname><respvalue>SOMENEWVALUE</respvalue></response>
<ldapname>LDAP-C</ldapname><response><respname>MY_URL</respname><respvalue>ldap:abc:xxxx</respvalue></response>
<response><respname>Name</respname><respvalue>MY_LDAP3</respvalue></response>
<response><respname>somethingelse</respname><respvalue>SOMEOTHERVALUE3</respvalue></response>
<response><respname>somethingelseotherdata</respname><respvalue>SOMEOTHERDATAVALUE3</respvalue> </response>
</body></html>
xsltproc generic1.xsl generic-test.xml:
LDAP-U URLldap://xxxx:yyy
名称我的\u LDAP1
SomeThingelSomeOtherValue1
LDAP-BMY_urldap:abc:xyz
命名我的\u目标\u LDAP
somethingelsomeothervalue2
有什么新闻价值吗
LDAP-CMY\u URL LDAP:abc:xxxx
命名我的\u LDAP3
某物某物某物价值3
SomeThingelOtherDataSomeOtherDataValue3
(注意:请不要注意上面的BODY和HTML标记……我实际上并没有使用HTML,但它只是让我在测试时更容易查看。)
在任何情况下,有人能建议我如何修改XSLT来完成我上面描述的工作吗
谢谢!
Jim如果我没弄错,你所需要做的就是: XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:value-of select="Configuration/Setting/Setting[Setting[@Name='Name']='MY_TARGET_LDAP']/@Name"/>
</xsl:template>
</xsl:stylesheet>
注意,这假设最多有一个设置
满足谓词中的条件。如果有更多,则只返回其中第一个的Name
属性的值
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:value-of select="Configuration/Setting/Setting[Setting[@Name='Name']='MY_TARGET_LDAP']/@Name"/>
</xsl:template>
</xsl:stylesheet>