我正在研究这个片段,但是,我得到了预期的输出。
这是我的输入
<?xml version="1.0" encoding="UTF-8" ?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader">
<EnvelopeVersion>1.0</EnvelopeVersion>
<GovTalkDetails>
<Keys/>
</GovTalkDetails>
<Body>
<NameSearch>
<ContinuationKey>fcb844eELdiGt/AO3sMH2IGP8Amoxy+wewviAdon</ContinuationKey>
<RegressionKey>fcb844eJyt0ttO20moxy+wewviAdon</RegressionKey>
<SearchRows>100</SearchRows>
<CoSearchItem>
<CompanyName>WILLIAM ROSE LTD</CompanyName>
<CompanyNumber>07905646</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
<CompanyDate></CompanyDate>
</CoSearchItem>
</NameSearch>
</Body>
</GovTalkMessage>
而且,这是我的xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="GovTalkMessage/Body/NameSearch/CoSearchItem">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
我的预期输出是
<CoSearchItem>
<CompanyName>WILLIAM ROSE LTD</CompanyName>
<CompanyNumber>07905646</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
<CompanyDate></CompanyDate>
</CoSearchItem>
请有人帮忙解决问题。
答案 0 :(得分:1)
应用的第一个模板是根节点,如果完全使用它们,则需要指定命名空间。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:g="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="/g:GovTalkMessage/g:Body/g:NameSearch/g:CoSearchItem"/>
</xsl:template>
</xsl:stylesheet>
<xsl:output/>
和<xsl:strip-space/>
只是为了修复缩进。
<强>输出:强>
<?xml version="1.0" encoding="UTF-8"?>
<CoSearchItem xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader">
<CompanyName>WILLIAM ROSE LTD</CompanyName>
<CompanyNumber>07905646</CompanyNumber>
<DataSet>LIVE</DataSet>
<CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
<CompanyDate/>
</CoSearchItem>
答案 1 :(得分:1)
由于您使用的是XSLT 2.0,因此您还可以使用xpath-default-namespace
属性。这样您就不必在xpath中使用前缀。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader"
xpath-default-namespace="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader"
version="2.0">
<xsl:template match="/">
<xsl:copy-of select="GovTalkMessage/Body/NameSearch/CoSearchItem"/>
</xsl:template>
</xsl:stylesheet>
答案 2 :(得分:0)
我已经使用以下内容来实现我想要的输出
<xsl:template match="/*[local-name()='GovTalkMessage']/*[local-name()='Body']/*[local-name()='NameSearch']/*[local-name()='CoSearchItem']">
<xsl:copy-of select="."/>
</xsl:template>
并且,它有效。谢谢大家。