What is the proper select attribute for this XSLT `<xsl:apply-templates>` tag?

StackOverflow https://stackoverflow.com/questions/21713890

  •  10-10-2022
  •  | 
  •  

Вопрос

I'm using xslt to render a soap response to json. I have no control over the soap service.

I'm able to use <xsl:apply-templates> to process a couple of elements, but I can't get one of the templates to work. I have it working using <xsl:for-each>.

Source XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
    <ActivityId CorrelationId="be49966e-745e-4701-902e-abcde759ecc6" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">89528cc1-ab87-4abf-b485-2f38a995ee54</ActivityId>
</s:Header>
<s:Body>
    <SearchDocumentsResponse xmlns="http://tempuri.org/">
        <SearchDocumentsResult xmlns:a="http://schemas.datacontract.org/2004/07/Flairdocs.Framework.DocumentManagement" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:StoredDocumentEntity>
                <a:CreatedBy>Data Migration Tool</a:CreatedBy>
                <a:CreatedDate>2014-01-16T00:00:00</a:CreatedDate>
                <a:Description/>
                <a:DocumentType>File SummaryCard (136)</a:DocumentType>
                <a:DocumentTypeSystemName>136_File Summary Card</a:DocumentTypeSystemName>
                <a:EntityAssociations xmlns:b="http://schemas.datacontract.org/2004/07/Flairdocs.Data.Models.Contracts.Data"/>
                <a:GenerationTemplateSystemName i:nil="true"/>
                <a:IsLink>false</a:IsLink>
                <a:LinkText i:nil="true"/>
                <a:ModifiedBy i:nil="true"/>
                <a:ModifiedDate>2014-01-16T00:00:00</a:ModifiedDate>
                <a:SummaryMetadata>
                    <a:DocumentMetaDataEntry>
                        <a:AttributeName>File #</a:AttributeName>
                        <a:DisplayInUi>false</a:DisplayInUi>
                        <a:DocumentMetaDataAttributeId>0</a:DocumentMetaDataAttributeId>
                        <a:MultiValueSeperator i:nil="true"/>
                        <a:SystemName>FileNumber</a:SystemName>
                        <a:Value>H0782-10014</a:Value>
                    </a:DocumentMetaDataEntry>
                    <a:DocumentMetaDataEntry>
                        <a:AttributeName>RW Process</a:AttributeName>
                        <a:DisplayInUi>false</a:DisplayInUi>
                        <a:DocumentMetaDataAttributeId>0</a:DocumentMetaDataAttributeId>
                        <a:MultiValueSeperator i:nil="true"/>
                        <a:SystemName>RWProcess</a:SystemName>
                        <a:Value>File Administration</a:Value>
                    </a:DocumentMetaDataEntry>
                    <a:DocumentMetaDataEntry>
                        <a:AttributeName>RW Project #</a:AttributeName>
                        <a:DisplayInUi>false</a:DisplayInUi>
                        <a:DocumentMetaDataAttributeId>0</a:DocumentMetaDataAttributeId>
                        <a:MultiValueSeperator i:nil="true"/>
                        <a:SystemName>RWProjectNumber</a:SystemName>
                        <a:Value>H0782</a:Value>
                    </a:DocumentMetaDataEntry>
                </a:SummaryMetadata>
                <a:Title>H0782-10014 - File Summary Card</a:Title>
                <a:VersionCount>1</a:VersionCount>
            </a:StoredDocumentEntity>
            <a:StoredDocumentEntity>
                <a:CreatedBy>Data Migration Tool</a:CreatedBy>
                <a:CreatedDate>2014-01-16T00:00:00</a:CreatedDate>
                <a:Description/>
                <a:DocumentType>File SummaryCard (136)</a:DocumentType>
                <a:DocumentTypeSystemName>136_File Summary Card</a:DocumentTypeSystemName>
                <a:EntityAssociations xmlns:b="http://schemas.datacontract.org/2004/07/Flairdocs.Data.Models.Contracts.Data"/>
                <a:GenerationTemplateSystemName i:nil="true"/>
                <a:IsLink>false</a:IsLink>
                <a:LinkText i:nil="true"/>
                <a:ModifiedBy i:nil="true"/>
                <a:ModifiedDate>2014-01-16T00:00:00</a:ModifiedDate>
                <a:SummaryMetadata>
                    <a:DocumentMetaDataEntry>
                        <a:AttributeName>File #</a:AttributeName>
                        <a:DisplayInUi>false</a:DisplayInUi>
                        <a:DocumentMetaDataAttributeId>0</a:DocumentMetaDataAttributeId>
                        <a:MultiValueSeperator i:nil="true"/>
                        <a:SystemName>FileNumber</a:SystemName>
                        <a:Value>H0782-10014</a:Value>
                    </a:DocumentMetaDataEntry>
                    <a:DocumentMetaDataEntry>
                        <a:AttributeName>RW Process</a:AttributeName>
                        <a:DisplayInUi>false</a:DisplayInUi>
                        <a:DocumentMetaDataAttributeId>0</a:DocumentMetaDataAttributeId>
                        <a:MultiValueSeperator i:nil="true"/>
                        <a:SystemName>RWProcess</a:SystemName>
                        <a:Value>File Administration</a:Value>
                    </a:DocumentMetaDataEntry>
                    <a:DocumentMetaDataEntry>
                        <a:AttributeName>RW Project #</a:AttributeName>
                        <a:DisplayInUi>false</a:DisplayInUi>
                        <a:DocumentMetaDataAttributeId>0</a:DocumentMetaDataAttributeId>
                        <a:MultiValueSeperator i:nil="true"/>
                        <a:SystemName>RWProjectNumber</a:SystemName>
                        <a:Value>H0782</a:Value>
                    </a:DocumentMetaDataEntry>
                </a:SummaryMetadata>
                <a:Title>H0782-10014 - File Summary Card</a:Title>
                <a:VersionCount>1</a:VersionCount>
            </a:StoredDocumentEntity>
        </SearchDocumentsResult>
    </SearchDocumentsResponse>
</s:Body>
</s:Envelope>

I need to pull out each document's main attributes and "flatten" the <a:DocumentMetaDataEntry> elements, like so:

Desired output

[{
"CreatedBy":"Data Migration Tool",
"CreatedDate":"2014-01-16T00:00:00",
"Description":"",
"DocumentType":"File SummaryCard (136)",
"DocumentTypeSystemName":"136_File Summary Card",
"EntityAssociations":"",
"GenerationTemplateSystemName":"",
"IsLink":"false",
"LinkText":"",
"ModifiedBy":"",
"ModifiedDate":"2014-01-16T00:00:00",
"Title":"H0782-10014 - File Summary Card",
"VersionCount":"1",
"FileNumber":"H0782-10014",
"RWProcess":"File Administration",
"RWProjectNumber":"H0782"
},{
"CreatedBy":"Data Migration Tool",
"CreatedDate":"2014-01-16T00:00:00",
"Description":"",
"DocumentType":"File SummaryCard (136)",
"DocumentTypeSystemName":"136_File Summary Card",
"EntityAssociations":"",
"GenerationTemplateSystemName":"",
"IsLink":"false",
"LinkText":"",
"ModifiedBy":"",
"ModifiedDate":"2014-01-16T00:00:00",
"Title":"H0782-10014 - File Summary Card",
"VersionCount":"1",
"FileNumber":"H0782-10014",
"RWProcess":"File Administration",
"RWProjectNumber":"H0782"
}]

The following XSLT gves me what I want, but I'd like to simplify it to remove the second <xsl:for-each> (and the first one if possible!)

Working XSLT

<?xml version="1.0"?>
<xsl:stylesheet xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tempuri.org/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://schemas.datacontract.org/2004/07/Flairdocs.Framework.DocumentManagement" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.datacontract.org/2004/07/Flairdocs.Data.Models.Contracts.Data" version="1.0">
<xsl:output method="text"/>

<xsl:template match="s:Header">
</xsl:template>

<xsl:template match="a:StoredDocumentEntity">
    <xsl:text>{</xsl:text>
        <xsl:for-each select="*[not(self::a:SummaryMetadata)]">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="local-name()"/>
            <xsl:text>":"</xsl:text>
            <xsl:value-of select="normalize-space(.)"/>
            <xsl:text>",</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="a:SummaryMetadata/a:DocumentMetaDataEntry">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="a:SystemName"/>
            <xsl:text>":"</xsl:text>
            <xsl:value-of select="a:Value"/>
            <xsl:text>"</xsl:text>
            <xsl:if test="following-sibling::*">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>         
    <xsl:text>}</xsl:text>
    <xsl:if test="following-sibling::*">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="s:Envelope">
    <xsl:text>[</xsl:text>
        <xsl:apply-templates select="@*|node()" />
    <xsl:text>]</xsl:text>
</xsl:template> 
</xsl:stylesheet>

I'd like to replace the second for-each with a template, something like the following, but I can't get it to match:

Nonfunctional XSLT

<?xml version="1.0"?>
<xsl:stylesheet xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://tempuri.org/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://schemas.datacontract.org/2004/07/Flairdocs.Framework.DocumentManagement" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.datacontract.org/2004/07/Flairdocs.Data.Models.Contracts.Data" version="1.0">
<xsl:output method="text"/>

<xsl:template match="s:Header">
</xsl:template>

<xsl:template match="a:DocumentMetaDataEntry">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="a:SystemName"/>
    <xsl:text>":"</xsl:text>
    <xsl:value-of select="a:Value"/>
    <xsl:text>"</xsl:text>
    <xsl:if test="following-sibling::*">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="a:StoredDocumentEntity">
    <xsl:text>{</xsl:text>
        <xsl:for-each select="*[not(self::a:SummaryMetadata)]">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="local-name()"/>
            <xsl:text>":"</xsl:text>
            <xsl:value-of select="normalize-space(.)"/>
            <xsl:text>",</xsl:text>
        </xsl:for-each>
        <xsl:apply-templates select="./a:DocumentMetaDataEntry" />
    <xsl:text>}</xsl:text>
    <xsl:if test="following-sibling::*">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:template>

<xsl:template match="s:Envelope">
    <xsl:text>[</xsl:text>
        <xsl:apply-templates select="@*|node()" />
    <xsl:text>]</xsl:text>
</xsl:template>

</xsl:stylesheet>

I know this is really simple but I've spent quite a bit of time on here and in other places and I just cant wrap my head around how templates work. What is the proper select attribute for the <xsl:apply-templates> tag?

Bonus ethereal elephants of awesome if I can replace the first for-each as well...

Это было полезно?

Решение

If your for-each loop works, you should be able to replace it with an apply-templates without changing the expression

<xsl:apply-templates select="a:SummaryMetadata/a:DocumentMetaDataEntry" />

The reason your current expression is not working is because it is looking for a child element called DocumentMetaDataEntry, when there is none as it is one further level down. Your expression needs to be this...

<xsl:apply-templates select=".//a:DocumentMetaDataEntry" />

The two slashes tells it to search for descendants at any level, not just the direct children.

And to replace your first xsl:for-each, just replace it with an xsl:apply-templates, like so

<xsl:apply-templates select="*[not(self::a:SummaryMetadata)]" />

Then, have a template that matches the children of StoredDocumentEntity like so.

<xsl:template match="a:StoredDocumentEntity/*">

Although this would match SummaryMetadata elements in theory, you don't actually have any apply-templates that select them.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top