从xml获取带有前缀的所有命名空间

时间:2015-07-20 10:59:02

标签: xml xslt

如何从xml获取所有xmlns?使用XSLT。 例如,我有xml:

    <?xml version="1.0" encoding="UTF-8"?>
<cult:ProjectDocCoordination xmlns:smev="http://smev.gosuslugi.ru/request/rev111111" xmlns:cult="urn://x-artefacts-it-ru/dob/state-services/cult/ProjectDocCoordination/1.0" xmlns:commons="urn://x-artefacts-it-ru/dob/state-services/cult/commons/1.0">
    <cult:Person>
        <commons:FamilyName>Иванов</commons:FamilyName>
        <commons:FirstName>Иван</commons:FirstName>
        <commons:Patronymic>Иванович</commons:Patronymic>
        <cult:OGRNIP>300000000000000</cult:OGRNIP>
        <cult:Phone>9212345678</cult:Phone>
        <cult:Email>test@mail.ru</cult:Email>
        <cult:Address>Address</cult:Address>
    </cult:Person>
    <cult:ObjectRegion>ObjectRegion</cult:ObjectRegion>
    <cult:ObjectName>ObjectName</cult:ObjectName>
    <cult:ScientificDesignDeveloperInfo>
        <cult:ProjectDocName>ProjectDocName</cult:ProjectDocName>
        <cult:ProjectDocComposition>ProjectDocComposition</cult:ProjectDocComposition>
        <cult:DeveloperLegalInfo>
            <cult:DeveloperULName>ООО Удача</cult:DeveloperULName>
            <cult:DeveloperULOPF>DeveloperULOPF</cult:DeveloperULOPF>
            <cult:Address>Address</cult:Address>
        </cult:DeveloperLegalInfo>
    </cult:ScientificDesignDeveloperInfo>
    <cult:LicenseInfo>
        <cult:Number>001877</cult:Number>
        <cult:Date>2014-06-06</cult:Date>
    </cult:LicenseInfo>
    <test:JobInfo xmlns:test="http://test.test.com">
        <test:Number>67800</test:Number>
        <test:Date>2014-08-06</test:Date>
        <test:AgentName>Иванов Иван Иванович</test:AgentName>
        <test:Phone>9212345678</test:Phone>
    </test:JobInfo>
    <cult:NoChangedCharacteristics>true</cult:NoChangedCharacteristics>
    <cult:CapitalReconstruction>true</cult:CapitalReconstruction>
    <cult:ResultInfo>Выдать лично на руки</cult:ResultInfo>
    <cult:DocInfo>
        <cult:Document1>
            <smev:AppliedDocument smev:ID="ID_1">
                <smev:CodeDocument>String</smev:CodeDocument>
                <smev:Name>1.txt</smev:Name>
                <smev:Number>1</smev:Number>
                <smev:URL>1.txt</smev:URL>
                <smev:Type>text/plain</smev:Type>
                <smev:DigestValue>UjBsR09EbGhjZ0dTQUxNQUFBUUNBRU1tQ1p0dU1GUXhEUzhi</smev:DigestValue>
            </smev:AppliedDocument>
        </cult:Document1>
    </cult:DocInfo>
</cult:ProjectDocCoordination>

我想得到一些东西(separator = $):

xmlns:smev="http://smev.gosuslugi.ru/request/rev111111"$xmlns:cult="urn://x-artefacts-it-ru/dob/state-services/cult/ProjectDocCoordination/1.0"$xmlns:commons="urn://x-artefacts-it-ru/dob/state-services/cult/commons/1.0"$xmlns:test="http://test.test.com"

或者这个(没有&#34; xmlns:&#34;):

smev="http://smev.gosuslugi.ru/request/rev111111"$cult="urn://x-artefacts-it-ru/dob/state-services/cult/ProjectDocCoordination/1.0"$commons="urn://x-artefacts-it-ru/dob/state-services/cult/commons/1.0"$test="http://test.test.com"

1 个答案:

答案 0 :(得分:0)

您不能在此处使用distinct-values(),因为这样您只会获得URI值并丢失前缀。此外,可能有两个或多个前缀链接到同一个URI - 反之亦然。

请尝试这种方式:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:for-each-group select="//namespace::*[not(name()='xml')]" group-by="concat(name(), '|', .)">
        <xsl:text>xmlns:</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>="</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"&#10;</xsl:text>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

<强>结果

xmlns:smev="http://smev.gosuslugi.ru/request/rev111111"
xmlns:cult="urn://x-artefacts-it-ru/dob/state-services/cult/ProjectDocCoordination/1.0"
xmlns:commons="urn://x-artefacts-it-ru/dob/state-services/cult/commons/1.0"
xmlns:test="http://test.test.com"

根据需要选择自己的分隔符。