I가 다음과 같은 XML 문서 내가 XSLT 템플릿 시도XSLT 노드 카운터
<root>
<TotalPeople>2</TotalPeople> <!-- Needs to calculate how many "Person" tags -->
<MoreTagsWithData/>
<Person>
<id>1</id> <!-- incrementing per each Person -->
<Name>John Smith</Name>
<MoreTagsWithData/>
</Person>
<Person>
<id>2</id>
<Name>John Doe</Name>
<MoreTagsWithData/>
</Person>
</root>
문서를 얻을 필요가
<root>
<TotalPeople>BLABLA</TotalPeople>
<MoreTagsWithData/>
<Person>
<id>bla-bla</id>
<Name>John Smith</Name>
<MoreTagsWithData/>
</Person>
<Person>
<id>bla-bla</id>
<Name>John Doe</Name>
<MoreTagsWithData/>
</Person>
</root>
:
<!-- copy all file (I need to save the whole file since in reality it contains much more data -->
<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- indexing ids -->
<xsl:template match="id">
<xsl:copy>
<xsl:number level="any"/>
</xsl:copy>
</xsl:template>
<!-- Piece I am not sure , it does not work -->
<xsl:template match="TotalPeople">
<xsl:copy>
<xsl:number level="any" count="/root/Person/id"/>
</xsl:copy>
</xsl:template>
내가 필요를 문서에서 얼마나 많은 태그를 계산하고이 값으로 내 문서의 특수 태그를 수정하십시오. 실제 문서에는 유지 관리해야 할 많은 정보가 포함되어 있으므로 새 문서를 만들 수 없습니다.
<xsl:template match="TotalPeople">
<xsl:copy>
<xsl:value-of select="count(/root/Person/id)"/>
<!-- or just select="count(/root/Person)" -->
</xsl:copy>
</xsl:template>
XSLT 스타일 시트는 현재 위치에서 문서를 편집 할 수 없습니다, 별도의 출력 문서에 입력 XML 문서를 변환하도록 설계되어 다음과 같이
흥미 롭 (나는 단지 색슨-HE 9.4. 이것을 테스트). 입력 된 문서를 덮어 쓰면 원본을 읽을 때까지 덮어 쓰기를 시작할 수 있습니까? – LarsH
@LarsH - 확실하지 않습니다. 스트리밍 중이 아니기 때문에 처리/덮어 쓰기가 시작되기 전에 전체 입력을 읽고 메모리에 저장해야한다고 생각합니다. 난이 두 번 해본 적이 있고 두 번 스택 오버플로 답변했다. 테스트 할 때 어떤 문제도 발생하지 않았지만 데이터 세트는 작았습니다. –